HasMany
CQL Active Record: HasMany
Relationship Guide
HasMany
Relationship GuideIn 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.
What is a HasMany
Relationship?
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.
Example Scenario: Posts and Comments
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.
Defining the Schema
We’ll define the posts
and comments
tables in the schema using CQL’s DSL.
posts table: Stores post details like
title
,body
, andpublished_at
.comments table: Stores comment details with a foreign key
post_id
that references theposts
table.
Defining the Models
Let’s db_context the Post
and Comment
models and establish the HasMany
and BelongsTo
relationships in CQL.
Post Model
The
has_many :comments
association in thePost
model defines that each post can have multiple comments.
Comment Model
The
belongs_to :post
association in theComment
model links each comment to a post by using thepost_id
foreign key.
Creating and Querying Records
Now that we have defined the Post
and Comment
models with a HasMany
and BelongsTo
relationship, let’s create and query records in CQL.
Creating a Post and Comments
First, we create a
Post
and save it to the database.Then, we create two
Comments
and associate them with the post by passingpost.id
as thepost_id
for each comment.
Accessing Comments from the Post
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.
Accessing the Post from a Comment
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.
Updating and Deleting the Associations
Adding a New Comment to an Existing Post
You can add a new comment to an existing post as follows:
Deleting a Post and Its Associated Comments
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.
Advanced Querying
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.
Fetching Posts with Comments
You can load posts along with their comments in one query:
Finding Comments for a Specific Post
If you want to query for specific comments associated with a post, you can filter them as follows:
Summary
In this guide, we’ve explored the HasMany
relationship in CQL. We:
Defined the
Post
andComment
tables in the schema.Created corresponding models, specifying the
HasMany
relationship in thePost
model and theBelongsTo
relationship in theComment
model.Demonstrated how to create, query, update, and delete records using the
HasMany
andBelongsTo
associations.
Next Steps
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.
Last updated