Ruby on rails 如何向include生成的联接添加附加条件?
我想在Ruby on rails 如何向include生成的联接添加附加条件?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我想在ActiveRecordfinder中的:include选项生成的左侧外部联接中添加其他条件 class Post has_many :comments end class Comment belongs_to :post has_many :comment_votes end class CommentVote belongs_to :comment end 现在让我们假设我想找到最后10篇文章及其相关评论和投票结果 Post.find.all(:limit =
ActiveRecord
finder中的:include
选项生成的左侧外部联接中添加其他条件
class Post
has_many :comments
end
class Comment
belongs_to :post
has_many :comment_votes
end
class CommentVote
belongs_to :comment
end
现在让我们假设我想找到最后10篇文章及其相关评论和投票结果
Post.find.all(:limit => 10, :order => "created_at DESC",
:include => [{:comments => :comment_votes])
我不能添加条件来检查up投票,因为它将忽略没有up投票的帖子。因此,条件必须转到为注释\u投票
生成的联接的ON子句。我希望使用以下语法:
Post.find.all(:limit => 10, :order => "created_at DESC",
:include => [{:comments => [:comment_votes, :on => "comment_votes.vote > 0"])
你以前遇到过这样的问题吗?您是否使用当前查找器解决了问题?我希望从社区听到一些有趣的想法
PS:我可以编写一个连接SQL来获得预期的结果并将结果缝合在一起。我想在走这条路之前确保没有其他选择。如果我正确地遵循了你的问题,类似的方法可能会奏效:
class Comment
belongs_to :post
has_many :comment_votes
has_many :up_votes, :class_name => "CommentVote", :conditions => "vote > 0"
end
那么你的发现者将是:
Post.find.all(:limit => 10, :order => "created_at DESC",
:include => {:comments => :up_votes})
注意:我没有测试这个