Ruby on rails 在集合和父级上使用.where创建范围

Ruby on rails 在集合和父级上使用.where创建范围,ruby-on-rails,postgresql,ruby-on-rails-5,Ruby On Rails,Postgresql,Ruby On Rails 5,我有三个模型,分别是父关系、父关系和子关系:组织、类别、发布 我试图在我的Post模型中创建一个作用域,首先在传递的集合上使用where,然后在父代上使用: scope :ready, -> { where("next_setup_at < ?", DateTime.current) .joins(category: :organization) .where("organizations.active = ?", true) } 作用域:就绪,->{ 其中(“

我有三个模型,分别是父关系、父关系和子关系:
组织
类别
发布

我试图在我的
Post
模型中创建一个作用域,首先在传递的集合上使用
where
,然后在父代上使用:

scope :ready, -> {
  where("next_setup_at < ?", DateTime.current)
    .joins(category: :organization)
    .where("organizations.active = ?", true)
}
作用域:就绪,->{
其中(“下一次设置时间<?”,DateTime.current)
.joins(类别::组织)
.where(“organizations.active=?”,true)
}
但博士后给了我一个错误:

ActiveRecord::语句无效:PG::AmbiguousColumn:错误:列引用“next\u setup\u at”不明确 第1行:…组织“.”id“=”类别“.”组织id“其中(下一个设置。。。 ^

:从“类别”上的“发布”内部连接“类别”,“id”=“发布”,“组织”上的“类别id”内部连接“组织”,“id”=“类别”,“组织id”中选择“发布”。*其中(下一个设置位置为<'2016-03-22 15:57:19.971887')和(organizations.active='t')


看看你的
.where
子句。第二个子句很好地定义了要查询的列

where("organizations.active = ?", true)
第一个没有

where("next_setup_at < ?", DateTime.current)
进一步改进

您可以很容易地在纯ActiveRecord中指定要引用的表,如下所示:

where(posts: {next_setup_at: DateTime.current}, categories: {active: true})

where
方法使用表名,而不是关系名。
where(posts: {next_setup_at: DateTime.current}, categories: {active: true})