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 コメント ON comments.topic_id = topic.id', :group => 'comments .topic_id HAVING count(comments.topic_id) < 5' )

于 2012-08-22T02:11:46.533 に答える