2012-08-22 4 views
0

저는 Rails 2.3.8 Ruby 1.8.7과 PostgreSQL을 개발 중입니다.has_many 모델 검색 크기로 찾기

특정 크기보다 작은 모델을 검색하려면 검색 조건을 작성하는 방법을 알고 싶습니까?

예를 들어, 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)]) 
+0

도움이 될까요? http://lukaszwrobel.pl/blog/sql-having-clause-in-rails-activerecord "선택"과 "함께"사용해야합니다. – crnlx

답변

0

이 문제가 해결됩니까?

Topic.find (모든, => '주제 *, AS COMMENT_COUNT) (comments.id를 계산합니다.'를 선택 => 'INNER comments.topic_id = topics.id에 대한 의견 가입하세요', 을 조인 : group => 'comments.topic_id 카운트 (comments.topic_id) < 5'