ActiveRecord::Relation code displaying instead of intended data

activerecord, associations, controller, nested-attributes, ruby-on-rails

Solution

You're probably rendering the actual review objects rather than a string representation of them. Something like this should work in the view:

<%= @ratings.map { |rating| rating.rating_count }.join(", ") %>

That turns your collection of ratings into a collection of rating_counts and then joins them with commas.

I can't help with the bug up the nose though :)

Problem

I'm trying to display a list of reviews on my home page and at the same time show the ratings for each review. I want a comma separated list of the different ratings attached to each review (based on the associated review_id); ratings are integers of -5 to 5. All I get when I load the page is this: ``` Review 1. #<ActiveRecord::Relation:0x007f9879749a50> Review 2. #<ActiveRecord::Relation:0x007f9879749a50> Review 3. #<ActiveRecord::Relation:0x007f9879749a50> Review 4. #<ActiveRecord::Relation:0x007f9879749a50> Review 5. #<ActiveRecord::Relation:0x007f9879749a50> ``` When I want to see this: ``` Review 1. 1, 5, 2, -3 Review 2. 2, -1 Review 3. 1, 1, 4, 5 Review 4. -5, -2, -3 Review 5. 1, 1, 3, 2, 1, 5, 3, 2 ``` user model: ``` class User < ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :ratings_attributes, :reviews_attributes has_many :reviews, dependent: :destroy has_many :ratings, :through => :reviews, dependent: :destroy accepts_nested_attributes_for :ratings, allow_destroy: true accepts_nested_attributes_for :reviews, allow_destroy: true end ``` review model: ``` class Review < ActiveRecord::Base attr_accessible :content, :priority, :ratings_attributes belongs_to :user has_many :ratings end ``` rating model: ``` class Rating < ActiveRecord::Base attr_accessible :rating_count, :review_id, :timestamp, :user_id belongs_to :user belongs_to :review end ``` home controller: ``` class HomeController < ApplicationController def index @reviews = current_user.reviews.order("position") @ratings = Rating.where(params[:review_id]) @ratings_list = Rating.find(:all) end ``` index.html.erb ``` <h1>Listing reviews</h1> <p id="notice"><%= notice %></p> <br /> <div class="span12"> <ul id="reviews" data-update-url="<%= sort_reviews_url %>"> <% @reviews.each do |review| %> <%= content_tag_for :li, review do %> <span class="handle">[drag]</span> <%= link_to h(review.content), review %> <!-- this is where i want a comma separated list of the different ratings attached to each review; ratings are integers of -5 to 5. --> <%= @ratings %> <% end %> <% end %> </ul> <br /> </div> <!-- for debugging that i'm actually getting through to the ratings data --> <%= @ratings_list %> <br /> <%= link_to 'New Review', new_review_path %> ``` For the life of me, I can't figure out if I'm doing something wrong in the controller or here in the html. All that displays next to each review is something like this: ``` #<ActiveRecord::Relation:0x007f987a9d7618> ``` Thanks in advance...and if you care at all, i'm working outside and a bug just flew up my nose.

Original source