LEFT OUTER JOIN
:include
中的选项
ActiveRecord
寻找者。
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])
我不能添加条件来检查向上投票,因为它将忽略没有向上投票的职位。因此,条件必须转到为
comment_votes
. 我希望有一个语法,如:
Post.find.all(:limit => 10, :order => "created_at DESC",
:include => [{:comments => [:comment_votes, :on => "comment_votes.vote > 0"])
你以前遇到过这样的问题吗?你用取景器解决了这个问题吗?我希望从社区听到一些有趣的想法。
附言:
我可以编写一个joinsql来获得预期的结果并将结果缝合在一起。我要确保在走这条路之前没有其他选择。