Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails中评级最高的评论_Ruby On Rails_Ruby_Rails Models - Fatal编程技术网

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) %>
#产品展示页面