是否有办法执行.includes并指定左外连接。
最初:
@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 :(得分: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