Has Many
Last updated
Last updated
In this guide, we’ll focus on the HasMany
relationship using CQL's Active Record syntax. Like the previous BelongsTo
and HasOne
relationships, we’ll start with an Entity-Relationship Diagram (ERD) to visually explain how the HasMany
relationship works and build on our previous schema.
HasMany
Relationship?The HasMany
relationship indicates that one entity (a record) is related to multiple other entities. For example, a Post can have many Comments. This relationship is a one-to-many mapping between two entities.
In a blogging system:
A Post can have many Comments.
Each Comment belongs to one Post.
This is a common one-to-many relationship where one post can have multiple comments, but each comment refers to only one post.
We’ll define the posts
and comments
tables in the schema using CQL’s DSL.
posts table: Stores post details like title
, body
, and published_at
.
comments table: Stores comment details with a foreign key post_id
that references the posts
table.
Let’s db_context the Post
and Comment
models and establish the HasMany
and BelongsTo
relationships in CQL.
The has_many :comments
association in the Post
model defines that each post can have multiple comments.
The belongs_to :post
association in the Comment
model links each comment to a post by using the post_id
foreign key.
Now that we have defined the Post
and Comment
models with a HasMany
and BelongsTo
relationship, let’s create and query records in CQL.
First, we create a Post
and save it to the database.
Then, we create two Comments
and associate them with the post by passing post.id
as the post_id
for each comment.
Once a post has comments, you can retrieve all the comments using the HasMany
association.
Here, post.comments
retrieves all the comments associated with the post, and we loop through them to print each comment’s body.
You can also retrieve the post associated with a comment using the BelongsTo
association.
In this example, comment.post
fetches the post that the comment belongs to.
You can add a new comment to an existing post as follows:
If you delete a post, you may want to delete all associated comments as well. However, by default, this will not happen unless you specify cascade deletion in your database.
You can also perform advanced queries using the HasMany
relationship. For example, finding posts with a certain number of comments or filtering comments for a post based on specific conditions.
You can load posts along with their comments in one query:
If you want to query for specific comments associated with a post, you can filter them as follows:
In this guide, we’ve explored the HasMany
relationship in CQL. We:
Defined the Post
and Comment
tables in the schema.
Created corresponding models, specifying the HasMany
relationship in the Post
model and the BelongsTo
relationship in the Comment
model.
Demonstrated how to create, query, update, and delete records using the HasMany
and BelongsTo
associations.
In the next guide, we’ll build upon this ERD and cover the ManyToMany
relationship, which is useful when two entities are associated with many of each other (e.g., a post can have many tags, and a tag can belong to many posts).
Feel free to experiment with the HasMany
relationship by adding more fields, filtering queries, or extending your schema to handle more complex use cases.