Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Active Record Associations in Rails Using Associations in Your App Rendering Collections

Active Record render quiz question

As we set up in previous challenges, an Owner model has_many Pet instances. In the views/owners/show.html.erb template, an Owner object is available within the @owner instance variable. Using the template file and the views/pets/_pet.html.erb partial, render a view that includes the name attribute for each Pet belonging to @owner.

I've tried : <%= render @pets.name %> in the (views/owners/show.html.erb) and <%= pet.name %> in the (views/pets/_pet.html.erb)

but i get a major unreadable error. I think im still a little murky on exactly what im trying to do here. has anyone solved this problem?

app/views/owners/show.html.erb
<h1>Owner: <%= @owner.name %></h1>

<div id="pets">
  <h2>Pets</h2>
  <%= render @pets.name %>

</div>
app/views/pets/_pet.html.erb
<div>
  <strong>Name:</strong>
  <%= # YOUR CODE HERE %>
</div>

4 Answers

Hi Paige,

I've not done this course so am unaware of the structure of the project or what previous tasks were about.

But, I had a go at this challenge and got an answer to pass. It doesn't use the partial, unfortunately, but I'm sure your prior knowledge of the course will let you implement the usage of that file.

I created an each loop in the view. In this, I took the @owner instance and looped over each element in the pets property. I then output the name attribute of the loop variable, which I called p, because I lack imagination.

That all looks like:

<h1>Owner: <%= @owner.name %></h1>

<div id="pets">
  <h2>Pets</h2>
  <!-- YOUR CODE HERE -->
  <% @owner.pets.each do |p| %>
    <%= p.name %>
  <% end %>
</div>

I hope that helps you out a little.

Steve.

it did! thank you

Excellent - glad to have helped!! :+1: :smile:

Steve just curious, were you working in web development before treehouse? Or did you learn via treehouse and then get hired as a moderator? Just curious as i'm about 3 months away from job search after I finish my Flatiron School Immersive Full Stack Bootcamp ( I use treehouse to supplement my learning and have loved it)

Hi Paige,

I have my own business, some of which is geared towards web development and other areas of the tech field. I have been involved in computers since I was very young; and now I am very old! But my main employment hasn't been as a developer - I'm a subject matter expert in other fields.

I used Treehouse to learn new things in different ways; I've found it a really useful resource. The moderator position is voluntary, not paid. I'm just a student like everyone else on here - the Treehouse staff have a green highlight on their profile picture with the word 'staff' too. But I like to help out on these pages as it really builds the learning experience trying to articulate problems to other people.

I hope your bootcamp goes well. Remember to take notes and get involved - and keep practicing! We forget stuff we don't do a lot so have a weekly dojo of refresher topics to keep everything alive in your mind. I try to do this a lot.

For other learning resources, there's some good Rails courses on Udemy. And if you want my 'one thing I wished I knew years ago' - learn to write tests. For Rails, look at RSpec. For Java, look at JUnit. Testing is so important in the development world but it gets ignored. Behaviour driven development is where it's at - so learn to define tests.

Good luck in your studies and shout if I can help with anything. You can @ mention me on here, or look me up on social media - I'm generally @onlysteveh on most platforms!

Steve.

Seth Kroger
Seth Kroger
56,413 Points

It should be <%= render @owner.pets %> in the (views/owners/show.html.erb) because pets doesn't exist on its own. It is a property of @owner

Adriana Cabrera
Adriana Cabrera
14,618 Points

I just want to share what works for me. I tried to copy whatever was in the treehouse video exercise

This activity <h1>Owner: <%= @owner.name %></h1> <div id="pets"> <h2>Pets</h2> <%= render @owner.pets %> </div>

Treehouse video post and comments <div id="comments"> <h1>Comments</h1> <%= render @post.comments %> </div>

This activity <div> <strong>Name:</strong> <%= pet.name %> </div>

Treehouse video post and comments <div> <strong><%= comment.name %> says:</strong> <%= comment.content %> </div> I hope it helps

Hello,

I wanted to try and explain this because I am not entirely sure if I get it 100% but hopefully it helps someone. I think the answer to this question is correct, but it is not using the partial method that the original question is asking.

Basically the question wants us to understand that it is good practice to use partial views to make the code more readable.

The output of the code should be the owner's pet's name.

The partial view is views/pets/_pet.html.erb_ You can tell it is a partial view by the underscore before pet.html.erb

<%= render @owner.pets %>

The line above is a short cut way of rendering the partial view, the way I think someone should look at the problem is to ask themselves; how does the line above actually execute? How does it know where to look for the partial view?

The answer is because rails takes the @owner.pets and constructs the directory of where the partial layout is from the .pets creates-> pets/_pet.html.erb. So the first part is done, you have hooked up a partial view to a view.

Note that within the render @owner.pets, it is also making a local variable pet, which is used in the partial view and necessary for the second part of this question.

The second part of the question asks to display the pets name's through the partial view. Like I stated above, the local variable is pet which is the collection of pet names that belong to the owner, therefore you can simply call the method name and it will display all of the dogs names that belong to the owner.

<%= pet.name %> would go into the partial view.

Hopefully this help someone out a little bit. I think the video prior to this challenge goes through this, but it is a little too fast, and hard to grasp. Not sure if I've done any better.