From the course: Ruby on Rails 6 Essential Training: Models and Associations
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Has and belongs to many relationships - Ruby on Rails Tutorial
From the course: Ruby on Rails 6 Essential Training: Models and Associations
Has and belongs to many relationships
- [Instructor] Let's spend some time working with Has and Belongs to Many relationships. The first question you may want to know is how to know when to use them. You want to use them when an object has many objects that belong to it, but that don't belong to it exclusively. So we have a project that has many collaborators but those collaborators are not exclusive to this project. They can collaborate on other projects too. Or we have a blog post and it has many categories but those categories are not exclusive to the blog post. It doesn't own them in any way. Instead the categories can also be assigned to other blog posts. As we saw in the introduction to this chapter, the Has and Belongs to Many relationship requires a join table between two other tables. That join table holds two foreign keys and the database best practice is to index both those keys together. This join table is going to do its work behind the scenes.…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Work with associations7m 35s
-
(Locked)
Validate presence with belongs_to2m 39s
-
(Locked)
Destroy dependent-related records3m 10s
-
(Locked)
Has and belongs to many relationships10m 3s
-
(Locked)
Rich join associations6m 28s
-
(Locked)
Traversing a rich join association3m 38s
-
(Locked)
Joining tables during queries6m 51s
-
(Locked)
-