Simple Many to Many Association
Greetings Geeks,
We'll begin by looking at a simple join, and then move on to a more complex join in the next movie. Many-to-many associations are similar to one-to-many associations because they also have an object which has many objects which belong to it. The difference that as a many-to-many, the objects don't belong to it exclusively. For example, a project has_and_belongs_to_many :collaborators. The project can have many collaborators, but the collaborators can also have many projects.
It's not like the project owns a collaborator exclusively. They're allowed to work on other projects, too. You have a Blog Post, which has_and_belongs_to_many :categories. If our Blog Post is both in the technology and training categories, it doesn't mean that no other Blog Post can be in the technology and training categories. Technology will have many Blog Posts. Training will have many Blog Posts. And several Blog Posts can make use of those other categories. When we talk about many-to-many, we're essentially talking about having two different sets: a set of projects and a set of collaborators.
And then we have a mix and match of relationships between those. It's gonna be very similar to working with a one-to-many association, but it's a one-to-many association from both sides at the same time. And as we saw back in the introduction to the chapter, that creates a problem as to where to put the foreign key. We need to put it in a join table, and the join table is going to hold two foreign keys that are going to relate to the two different sides of the association. We're gonna put an index on both of those keys so that we can look them up very quickly.
The table doesn't need to have any kind of primary key on it because we're not gonna be looking up those records directly. We're only gonna be using them as links to maintain the relationship between two other classes. When we add has_and_belongs_to_many in our models, Active Record will add instance methods that are just like the ones that are used by one-to-many. And this makes sense because we're still working with arrays of objects on both sides. So the methods will be the same, and Rails will handle all the behind-the-scenes differences for us. In our simple CMS, we're gonna use many-to-many relationships between Admin User and page.
What I have in mind is that certain Admin Users will be able to edit a page. So there will be a relationship between a page and the users who can edit the page. It won't be an exclusive relationship because each of those users also has the ability to edit other pages. So we have Admin User has_and_belongs_to_many :pages and a page has_and_belongs_to_many :admin_users. Just like with our other associations,be sure that you define both sides of the relationship so that you can move both ways from Admin User to pages and from page back to Admin Users.
Before we can work on our models, we're going to need to create a migration for the join table used in this relationship. You can name your join table anything you want, but Rails has a convention that we should try and follow. Then you can configure it if you wanna use something different. The Rails convention is to use the name of the first table being joined, then an underscore, and then the second table name. Both table names are gonna be plural, and they're gonna be expressed in alphabetical order. That's important. Again, this is the default naming, and it can be configured.
So for example, if we had Project and Collaborator and those were our two classes, then presumably we have a Projects table and a Collaborators table, then the join table is gonna be collaborators_projects, collaborators being the first in alphabetical order, and then underscore, and then the second one, projects. If we have BlogPost and Category, then we're gonna have blog_posts, that's the name of the first table, and then underscore, and then categories. Again, alphabetical order.
Admin User-Page, which is the one we're gonna use, we're gonna have admin_users, and then underscore, pages. Just make sure they're both plural and in alphabetical order. Let's create a migration for this join table now. So if you remember how to create a migration from the root of our project, we're gonna call rails generate, and then migration, and then the name we wanna use for the migration,Create Admin Users PagesJoin. It can really be anything we want. We want it to be something that's descriptive. And here's gonna be our file.
CHEERS!!!
Maitrey Patel
Maitrey Patel
Comments
Post a Comment