Ruby on rails rails中评级最高的评论
我试图在产品展示页面中显示评分最高的评论,但它显示的是#而不是评论。你知道为什么吗Ruby on rails rails中评级最高的评论,ruby-on-rails,ruby,rails-models,Ruby On Rails,Ruby,Rails Models,我试图在产品展示页面中显示评分最高的评论,但它显示的是#而不是评论。你知道为什么吗 #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
#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 %>
#注释模型
类注释<应用程序记录
属于:用户
属于:产品
范围:rating_desc,->{order(rating::desc)}
范围:rating_asc,->{order(rating::asc)}
结束
#产品模型
类别产品<应用记录
有很多订单吗
有很多评论
def最高评分注释
注释.评级_desc.first
结束
结束
#产品展示页
如果您的输出看起来像“#”
,那么您看到的是调用到@product.highest\u rating\u comment的的结果。基本上你看到的是对象在内存中位置的文本表示
你可能想要的是评论的内容。由于您没有提供架构,我无法说出该字段的名称-可能是@product.highest\u rating\u comment.comment
如果您的输出看起来像“#”
,那么您看到的是调用到@product.highest\u rating\u comment
的结果。基本上你看到的是对象在内存中位置的文本表示
你可能想要的是评论的内容。由于您没有提供架构,我无法说出该字段的名称-可能是@product.highest\u rating\u comment.comment
它显示了inspect
方法的结果。您需要输出评级字段的值。向产品展示页面添加更改:
#product show page
<%= @product.highest_rating_comment.try(:rating) %>
#产品展示页面
它显示了检查方法的结果。您需要输出评级字段的值。向产品展示页面添加更改:
#product show page
<%= @product.highest_rating_comment.try(:rating) %>
#产品展示页面