2013-10-23 128 views
0

我有以下型号的关系:查找所有父母和祖父母都符合特定条件的孩子吗?

class Author 
    has_many :posts 
end 

class Post 
    belongs_to :author 
    has_many :comments 
end 

class Comment 
    belongs_to :post 
end 

我已经布尔列“活动”的作者和“发布”的帖子。

我想找个地方author.active所有评论:true和post.published:真

任何人都可以帮我吗?我能够得到与author.active一个作者的所有讯息:通过使用连接语句(此代码在Post模型)真:

joins(:author).where(authors: {active: true}) 

,但我似乎无法弄清楚如何获得所有评论,其中author.active:true和post.published:true。

回答

2

这应该工作

Comment.joins(:post => :author).where("authors.active = true AND posts.published = true") 

Comment.joins(:post => :author).where(:post => {:published => true, :author => {:active => true}}) 
+0

谢谢,这是加入这是搞乱我的哈希语法! – MichaelHajuddah