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.

Traversing a rich join association

Traversing a rich join association - Ruby on Rails Tutorial

From the course: Ruby on Rails 6 Essential Training: Models and Associations

Start my 1-month free trial

Traversing a rich join association

- I want to continue our discussion about the ways that we can do many to many relationships by talking about how we traverse rich join associations. First, let me explain why this is a topic we need to discuss. Let's say that we have a has and belongs to many relationship. Like we have with user and department. We can take a user object and we can call user.departments to get back in array of all of the departments that that user has been assigned. But with the rich joint that we just created, we can't do that. We have a rich join between page and users with user assignments in the middle. But page doesn't have a relationship to users. We didn't define anything in the page class to tell it about users. So pages.users won't work. Page.user_assignments will work. We've made that relationship. And then we could go through those page assignments and get back each and every one of the users. But that's a cumbersome…

Contents