2012-03-20 22 views
2

.includesを実行し、外部左結合を指定する方法はありますか。もともと外部の左結合(Rails含む)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) 

含む、使用することを除いては(私は複数の投稿があったと言っている場合)イーガーローディングの方法でそれを実行することになります。

先進的なヘルプありがとうございます。 ジャスティン

+0

私はここで、このような何かをした私はそれを達成する方法である: [スタックオーバーフロー] [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

私は、次の操作を行います:

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 

次に、あなたが行うことができるでしょう:

@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 
関連する問題