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
Ruby on rails 在ActiveRecord-RubyonRails中选择distinct by列_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 在ActiveRecord-RubyonRails中选择distinct by列

Ruby on rails 在ActiveRecord-RubyonRails中选择distinct by列,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有两张桌子: 1. Post table (:id, :content) 2. Comment table (:id, :post_id, :content) 我正在使用以下代码: @result = Comment.where("content LIKE :query", query: "%#{@keyword}%") @result有更多重复的帖子,我如何通过DISTINCT:post\u id 谢谢你的帮助 请记录另一种解决方案 @result = Comment.where("co

我有两张桌子:

1. Post table (:id, :content)
2. Comment table (:id, :post_id, :content)
我正在使用以下代码:

@result = Comment.where("content LIKE :query", query: "%#{@keyword}%")
@result
有更多重复的帖子,我如何通过
DISTINCT:post\u id

谢谢你的帮助

请记录另一种解决方案

@result = Comment.where("content LIKE :query", query: "%#{@keyword}%").group(:post_id)

怎么会有重复的值?一篇文章可以有许多评论,其中post_id是相同的。一篇帖子可以有5条评论,包括文本“foo”和1条以上的评论@huanson