BoB BoB -4 years ago 115
Ruby Question

Highest rating comment in rails

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) }

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

def highest_rating_comment

#product show page
<%= @product.highest_rating_comment %>

Answer Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download