2012-03-20 3 views
2

.includes를 수행하고 외부 왼쪽 결합을 지정하는 방법이 있습니까?레일이 포함 된 외부 왼쪽 결합 3

원래

:

@post = Post.includes(:comments).where("comments.spam = ?", false).where(:id => params[:id]).first 
@comments = post.comments 

욕망은 모방하는 것입니다 사용하여 제외

@post = Post.find(params[:id]) 
@comments = post.comments.where(:spam => false) 

열망 로딩 방식으로 그것을 수행하는 것 포함 (I 여러 게시물을 가지고 말한다면).

도움을 주셔서 감사합니다. 저스틴

다음 작업을 수행 할 것
+0

나는 이런 식으로 뭔가를했다가, 여기에 있습니다 : : 그런 다음

class Post < ActiveRecord::Base has_many :comments has_many :non_spam_comments, :through => :comments, :conditions => {:spam => false} has_many :spam_comments, :through => :comments, :conditions => {:spam => true} end 

, 당신은 할 수 있습니다 [스택 오버플로] [1] [1] : http://stackoverflow.com/questions/15441012/any-possible-way-to-add-parameters-to-on-clause-on-include-left-joins-on-rails/16551544#16551544 –

답변

0

: 나는 그것을 달성하는 방법

@posts = Post.where(:user_id => current_user.id).includes(:non_spam_comments) 
@posts.each do |post| 
    post.non_spam_comments.each do |comment| 
    // Some comment operation 
    end 
end