0

我正在开发 Rails 2.3.8 Ruby 1.8.7 和 PostgreSQL。

当我想搜索小于特定大小的模型时,如何编写查找条件?

例如,我想搜索少于 5 条评论的主题。

class Topic < ActiveRecord::Base
     has_many :comments
end

class Comment < ActiveRecord::Base
     belongs_to :topic
end

Topic.find(:all,:include => :comments, :conditions => [(which has less than 5 comments)])
4

1 回答 1

0

这会解决你的问题吗?

Topic.find(:all, :select => 'topics.*, count(comments.id) AS comment_count', :joins => 'INNER JOIN comments ON comments.topic_id = topics.id', :group => 'comments .topic_id HAVING count(comments.topic_id) < 5' )

于 2012-08-22T02:11:46.533 回答