Displaying Has Many Through Rails
Objectives
Construct a bi-directional has many through.
Query for associations via the belongs_to, has_many, and has_many through associations.
Iterate over associations in a view and display associated data for a primary instance.
Identify the join model in a has many through.
Overview
We've seen how we can use simple associations to display data to our users in Rails, but what about more complex relationships? Lucky for us, the interface for displaying this type of data is just as easy thanks to Active Record and has_many, through
.
Lesson
has_many, through
Let's say you're making a blog and want to give users the ability to sign up and comment on your posts. What's the relationship between a post and a comment? If you said, "A comment belongs to a post, and a post has many comments," give yourself a pat on the back!
What about the relationship between a user and a comment? Again, a user has many comments, and a comment belongs to the user. So far, this is pretty straightforward.
Things get slightly more complicated when we talk about the relationship between a user and the posts that the user has commented on. How would you describe that relationship? Well, a user obviously can comment on many posts, and a post has comments from many users. Yep, this is a many to many relationship. We can set up a many-to-many relationship using a join table. In this case, comments
will act as our join table. Any table that contains two foreign keys can be thought of as a join table. A row in our comments
table will look something like this:
id
content
post_id
user_id
1
"I loved this post!"
5
3
For this instance, we know that the Comment
with an ID of 1
was created by the User
with an ID of 3
for the Post
with an ID of 5
. We have all of the information we need to determine all of the posts that a particular user has commented on as well as all of the users who commented on any post. When we're done, we'll be able to simply call @user.posts
to get a collection of all of those posts.
Let's set this up. First, we'll need migrations for comments
, posts
, and users
tables. We've included migrations and models in this repo, so you can follow along.
In our models, we have the following:
Notice that we can't just declare that our User
has_many :posts
because our posts
table doesn't have a foreign key called user_id
. Instead, we tell Active Record to look through the comments
table to figure out this association by declaring that our User
has_many :posts, through: :comments
. Now, instances of our User
model respond to a method called posts
. This will return a collection of posts that share a comment with the user.
Displaying Comments on Our Posts
Now that our association is set up, let's display some data. First, let's set up our Post#show
page to display all of the comments on a particular post. We'll include the username of the user who created the comment as well as a link to their show page.
In app/controllers/posts_controller.rb
, define a show
action that finds a particular post to make it available for display.
In our Post#show
page, we'll display the title and content information for the post as well as the information for each comment associated with the post.
This is the same as we've done before –– we're simply looking at data associated with posts and comments. Calling comment.user
returns for us the User
object associated with that comment. We can then call any method that our user responds to, such as username
.
Adding Posts to Our Users
Let's say that on our User#show
page we want our users to see a list of all of the posts that they've commented on. What would that look like?
Because we've set up a join model, the interface will look almost identical. We can simply call the posts
method on our user and iterate through.
Conclusion
Displaying data via a has_many, through
relationship looks identical to displaying data through a normal relationship. That's the beauty of abstraction –– all of the details about how our models are associated with each other get abstracted away, and we can focus simply on the presentation.
Clone https://github.com/learn-co-curriculum/displaying-has-many-through-rails
Last updated