Many To Many
Last updated
Last updated
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.
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).
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.
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
, and published_at
.
tags table: Stores tag names.
post_tags table: A join table that connects posts
and tags
via their foreign keys post_id
and tag_id
.
Let’s define the Post
, Tag
, and PostTag
models in CQL, establishing the ManyToMany
relationship.
In the Post
model, we define:
has_many :post_tags
to establish the association between Post
and the join table PostTag
.
has_many :tags, through: :post_tags
to associate Post
with Tag
through the join table.
Similarly, in the Tag
model, we db_context:
has_many :post_tags
to associate Tag
with PostTag
.
has_many :posts, through: :post_tags
to associate Tag
with Post
through the join table.
The PostTag
model links each Post
and Tag
by storing their respective IDs.
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.
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 the Post
with these two Tags
.
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.
Similarly, you can retrieve all posts associated with a tag.
Here, tag.posts
retrieves all the posts associated with the tag.
You can associate more tags with an existing post by creating new entries in the PostTag
join table.
To disassociate a tag from a post, you need to delete the corresponding record from the PostTag
join table.
You can also perform advanced queries using the ManyToMany
relationship, such as finding posts with a specific tag or fetching tags for multiple posts.
To find all posts associated with a specific tag, you can filter the posts by the tag name.
You can fetch all tags associated with multiple posts as follows:
In this guide, we explored the ManyToMany
relationship in CQL. We:
Define the Post
, Tag
, and PostTag
tables in the schema
Created corresponding models, specifying the ManyToMany
relationship between Post
and Tag
through the PostTag
join table.
Demonstrated how to create, query, update, and delete records in a ManyToMany
relationship.
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.