我正在尝试获取帖子,并且仅根据条件获取属于该帖子的评论:
即。
# Grab all posts but only include comments that have been approved.
Post.all(:include => :comments, :conditions => ['comments.approved = ?', true])
更新 2011 年 7 月 20 日 10:11 EST
澄清一下,我试图获取所有帖子,并且只获取特定用户对该帖子的评论。
def grab_posts_and_only_comments_from(user)
{:include => [:comments], :conditions => ['comments.user_id = ?', user.id]}
end
更新于美国东部时间 2011 年 7 月 20 日 11:34
在选中答案的评论中回答。
最佳答案
Post.includes(:comments).where("comments.approved = ?", true)
关于此功能的文档在 EdgeGuides 中得到了很大改进。查看第 12.2 节 here .
关于ruby-on-rails - 如何只有 :include based on a conditional statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6770515/