This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Active Record Associations in Rails!
You have completed Active Record Associations in Rails!
We can use all the same association methods we were using in the Rails console within controllers and views. That's going to be the key to allowing create, read, update, and delete actions on our associated models from a user's web browser. First, let's try applying that to viewing a list of the comments associated with a Post.
Back in our blog app, we can associate comments with a post in the Rails console. But users can't view a post's comments from their browser yet. Let's fix that.
We can use all the same association methods we were using in the Rails console within controllers and views. That's going to be the key to allowing create, read, update, and delete actions on our associated models from a user's web browser. First, let's try applying that to viewing a list of the comments associated with a Post.
Here's the most direct way:
<div id="comments">
<h1>Comments</h1>
<!-- The `comments` method returns an array-like collection, so we can call `each` on it: -->
<% @post.comments.each do |comment| %>
<div>
<strong><%= comment.name %> says:</strong>
<%= comment.content %>
</div>
<% end %>
</div>
Now you've got a working list of comments for each Post. This works okay for an associated model with only two attributes. But once you start working with more complex associated models, embedding their code in the view for a different model becomes unwieldy. Up next we'll look at a better way to break up your ERB code, using partials.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up