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