1

I'm trying to show the comment with the highest rating in the product show page but it shows # instead of the comment. Any ideas why?

#comment model
class Comment < ApplicationRecord
 belongs_to :user
 belongs_to :product

 scope :rating_desc, -> { order(rating: :desc) }
 scope :rating_asc, -> { order(rating: :asc) }
end

#product model
class Product < ApplicationRecord
  has_many :orders
  has_many :comments

  def highest_rating_comment
    comments.rating_desc.first
  end
end

#product show page
<%= @product.highest_rating_comment %>
BoB
  • 133
  • 1
  • 11

2 Answers2

0

If your output looks something like "#<Comment:0x007fb9ea9561d0>", then what you're seeing is the result of calling to_s on @product.highest_rating_comment. Basically you're seeing the text representation of the object's location in memory.

What you probably want instead is the contents of the comment. Since you didn't provide your schema, I can't say what that field is called - perhaps @product.highest_rating_comment.comment?

Brian
  • 5,300
  • 2
  • 26
  • 32
0

It shows a result of inspect method. You need to output a value of a rating field. Add changes to the product show page:

#product show page
<%= @product.highest_rating_comment.try(:rating) %>
Alex Kojin
  • 5,044
  • 2
  • 29
  • 31