ManyToMany
CQL Active Record: ManyToMany
Relationship Guide
ManyToMany
Relationship GuideIn this guide, we’ll cover the ManyToMany
relationship using CQL's Active Record syntax. This is a more complex relationship compared to HasOne
and HasMany
, and it’s commonly used when two entities have a many-to-many relationship, such as posts and tags where:
A Post can have many Tags.
A Tag can belong to many Posts.
To model this, we need an intermediate (or join) table that connects these two entities.
What is a ManyToMany
Relationship?
ManyToMany
Relationship?A ManyToMany
relationship means that multiple records in one table can relate to multiple records in another table. For example:
A Post can have multiple Tags (e.g., "Tech", "News").
A Tag can belong to multiple Posts (e.g., both Post 1 and Post 2 can have the "Tech" tag).
Example Scenario: Posts and Tags
We’ll use a scenario where:
A Post can have many Tags.
A Tag can belong to many Posts.
We will represent this many-to-many relationship using a join table called PostTags.
Defining the Schema
We’ll define the posts
, tags
, and post_tags
tables in the schema using CQL’s DSL.
posts table: Stores post details such as
title
,body
, andpublished_at
.tags table: Stores tag names.
post_tags table: A join table that connects
posts
andtags
via their foreign keyspost_id
andtag_id
.
Defining the Models
Let’s define the Post
, Tag
, and PostTag
models in CQL, establishing the ManyToMany
relationship.
Post Model
In the Post
model, we define:
has_many :post_tags
to establish the association betweenPost
and the join tablePostTag
.has_many :tags, through: :post_tags
to associatePost
withTag
through the join table.
Tag Model
Similarly, in the Tag
model, we db_context:
has_many :post_tags
to associateTag
withPostTag
.has_many :posts, through: :post_tags
to associateTag
withPost
through the join table.
PostTag Model (Join Table)
The PostTag
model links each Post
and Tag
by storing their respective IDs.
Creating and Querying Records
Now that we’ve db_contextd the Post
, Tag
, and PostTag
models, let’s explore how to create and query records in a ManyToMany
relationship.
Creating a Post with Tags
In this example:
We create a
Post
and save it to the database.We create two
Tags
("Tech" and "Programming") and save them.We create records in the
PostTag
join table to associate thePost
with these twoTags
.
Accessing Tags for a Post
Once a post has tags associated with it, you can retrieve them using the ManyToMany
association.
Here, post.tags
retrieves all the tags associated with the post.
Accessing Posts for a Tag
Similarly, you can retrieve all posts associated with a tag.
Here, tag.posts
retrieves all the posts associated with the tag.
Updating and Deleting the Associations
Adding New Tags to a Post
You can associate more tags with an existing post by creating new entries in the PostTag
join table.
Removing a Tag from a Post
To disassociate a tag from a post, you need to delete the corresponding record from the PostTag
join table.
Advanced Querying
You can also perform advanced queries using the ManyToMany
relationship, such as finding posts with a specific tag or fetching tags for multiple posts.
Fetching Posts with a Specific Tag
To find all posts associated with a specific tag, you can filter the posts by the tag name.
Fetching Tags for Multiple Posts
You can fetch all tags associated with multiple posts as follows:
Summary
In this guide, we explored the ManyToMany
relationship in CQL. We:
Define the
Post
,Tag
, andPostTag
tables in the schemaCreated corresponding models, specifying the
ManyToMany
relationship betweenPost
andTag
through thePostTag
join table.Demonstrated how to create, query, update, and delete records in a
ManyToMany
relationship.
Next Steps
This concludes our series of guides on relationships in CQL Active Record, covering BelongsTo
, HasOne
, HasMany
, and ManyToMany
. Feel free to experiment by extending your models, adding validations, or implementing more complex queries to suit your needs.
Last updated