Has One

In this guide, we'll cover the HasOne relationship using CQL's Active Record syntax. Like in the previous BelongsToguide, we'll start with an Entity-Relationship Diagram (ERD) to visually represent how the HasOne relationship works and build on the structure we already introduced with the BelongsTo relationship.

What is a HasOne Relationship?

The HasOne relationship indicates that one entity (a record) is related to exactly one other entity. For example, a User can have one Profile associated with it. This relationship is a one-to-one mapping between two entities.

Example Scenario: Users and Profiles

Let's say we have a system where:

  • A User can have one Profile.

  • A Profile belongs to one User.

We will represent this one-to-one relationship using CQL's HasOne and BelongsTo associations.


Defining the Schema

We'll define the users and profiles tables in the schema using CQL.

AcmeDB = CQL::Schema.define(
  :acme_db,
  adapter: CQL::Adapter::Postgres,
  uri: ENV["DATABASE_URL"]
) do
  table :users do
    primary
    text :name
    text :email
  end

  table :profiles do
    primary
    bigint :user_id, index: true
    text :bio
    text :avatar_url
  end
end
  • users table: Stores user details like name and email.

  • profiles table: Stores profile details like bio and avatar_url. It has a user_id foreign key referencing the userstable.


Defining the Models

Let's define the User and Profile models in CQL, establishing the HasOne and BelongsTo relationships.

User Model

  • The has_one :profile, Profile association in the User model indicates that each user has one profile. The foreign key (e.g., user_id) is expected on the profiles table.

Profile Model

  • The belongs_to :user, User, foreign_key: :user_id association in the Profile model links each profile to a user.

Creating and Querying Records

Now that we have defined the User and Profile models with has_one and belongs_to relationships, let's see how to create and query records.

Creating a User and Profile

Option 1: Create User, then use create_association for Profile

Option 2: Create User, then build and save Profile manually

Option 3: Manual creation (less common with helpers available)

Accessing the Profile from the User

Once a user and their profile have been created, you can retrieve the profile using the has_one association.

Here, user.profile fetches the profile associated with the user.

Accessing the User from the Profile

Similarly, you can retrieve the associated user from the profile.

In this example, profile.user fetches the User associated with that Profile.

Deleting the Profile

You can also delete the associated profile.

Similarly, deleting the user will not automatically delete the associated profile unless cascade rules are explicitly set in the database or handled by before_destroy callbacks on the User model.


Summary

In this guide, we explored the has_one relationship in CQL. We:

  • Define the User and Profile tables in the schema.

  • Created corresponding models, specifying the has_one relationship in the User model and the belongs_torelationship in the Profile model.

  • Demonstrated how to create, query, update, and delete records using the has_one and belongs_to associations.

Next Steps

In the next guide, we'll extend the ERD and cover the has_many relationship, which is commonly used when one entity is associated with multiple records (e.g., a post having many comments).

Feel free to experiment with the has_one relationship by adding more fields to your models, setting up validations, or extending your schema with more complex relationships.

Last updated

Was this helpful?