【问题标题】:Find all Children where Parent and Grandparent meet certain condition?找到父母和祖父母满足特定条件的所有孩子?
【发布时间】:2013-10-23 02:03:38
【问题描述】:

我有以下模型关系:

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: true 的所有 cmets

谁能帮帮我?我可以通过使用 join 语句(Post 模型中的此代码)从 Author.active: true 获取作者的所有帖子:

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

但我似乎无法弄清楚如何获取 author.active: true 和 post.published: true 的所有 cmets。

【问题讨论】:

    标签: ruby-on-rails activerecord


    【解决方案1】:

    这应该可以工作

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

    或

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

    【讨论】:

    • 谢谢,是连接哈希语法搞砸了我!
    猜你喜欢
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2013-09-03
    • 2011-01-24
    相关资源
    最近更新 更多