예 : Post
has_many Comments
.연관된 레코드가없는 레코드 찾기
의견이없는 모든 게시물을 찾는 방법은 무엇입니까? 사용, 더 나은 성능을 위해
Post.where(" not exists (select 'x' from comments where comments.post_id = posts.id)")
예 : Post
has_many Comments
.연관된 레코드가없는 레코드 찾기
의견이없는 모든 게시물을 찾는 방법은 무엇입니까? 사용, 더 나은 성능을 위해
Post.where(" not exists (select 'x' from comments where comments.post_id = posts.id)")
당신은하지와 같은 존재 사용할 수
belongs_to :post, :counter_cache => true
api.rubyonrails.org을 다음 Railscasts
당신이 할 수 있습니다 :
Post.where("comments_count = ?", 0)
counter_cache 열 :
이
하위 쿼리를 피하기 위해 카운터 캐시를 사용합니다. 많은 의견이 있으면 불필요하게 느려질 수 있습니다. – DanS