Rails5:如何使用ActiveRecord;或;用“查询”;包括“;? 类父级

Rails5:如何使用ActiveRecord;或;用“查询”;包括“;? 类父级,activerecord,ruby-on-rails-5,Activerecord,Ruby On Rails 5,我想获取数据“status1”或“status2有名为“ABC”的子对象,但出现了错误。采用另一个具有类似筛选模式的关系,并将其与正在调用的对象上已存在的筛选相结合 例如,Parent.status1.或(Parent.status2)将为您提供一组状态为status:1或status:2的记录 (如果有人不熟悉,问题中的示例也会使用,这允许使用值的名称筛选枚举的属性值。#status1和#status2在这种情况下分别对应于{status:0}和{status:1}) 为了调用更多的关系方法来

我想获取数据“status1”或“status2有名为“ABC”的子对象,但出现了错误。

采用另一个具有类似筛选模式的关系,并将其与正在调用的对象上已存在的筛选相结合

例如,
Parent.status1.或(Parent.status2)
将为您提供一组状态为
status:1
status:2
的记录

(如果有人不熟悉,问题中的示例也会使用,这允许使用值的名称筛选枚举的属性值。
#status1
#status2
在这种情况下分别对应于
{status:0}
{status:1}

为了调用更多的关系方法来修改最终结果,必须在调用
#或
的结果上调用它们,如下所示:

class Parent < ApplicationRecord
  has_many :children

  enum status: {
     status1: 0,
     status2: 1     
  }
end

class Child < ApplicationRecord
  belongs_to :parent
end

# error
# "Relation passed to #or must be structurally compatible. Incompatible values: [:references]"
combination = Parent.status1.or(Parent.status2.includes(:children).where(children: {name: 'ABC'}))

根据您的评论,我现在看到您希望记录(具有
status1
)或(具有
status2
,并且具有匹配的
子记录)

请注意,为了在
where
(如
where(子项:{name:value})
中使用关系,必须使用相关表(
连接(:子项)。其中(子项:{name:value})
。如果只使用
包含
,ActiveRecord似乎会推断连接,但据我所知,这并没有记录在案。这就是为什么
认为这两个关系不兼容的原因:一个在引用列表中有
子项,而另一个没有

如果手动将
where
子句编写为字符串,则不会更改引用列表,因此
不会将关系视为不兼容。手动编写
where
子句时,必须显式使用
联接

Parent.status1.or(Parent.status2).includes(:children).where(children: {name: 'ABC'})
采用另一个具有类似过滤器模式的关系,并将其与所调用对象上已存在的过滤器相结合

例如,
Parent.status1.或(Parent.status2)
将为您提供一组状态为
status:1
status:2
的记录

(如果有人不熟悉,问题中的示例也会使用,这允许使用值的名称筛选枚举的属性值。
#status1
#status2
在这种情况下分别对应于
{status:0}
{status:1}

为了调用更多的关系方法来修改最终结果,必须在调用
#或
的结果上调用它们,如下所示:

class Parent < ApplicationRecord
  has_many :children

  enum status: {
     status1: 0,
     status2: 1     
  }
end

class Child < ApplicationRecord
  belongs_to :parent
end

# error
# "Relation passed to #or must be structurally compatible. Incompatible values: [:references]"
combination = Parent.status1.or(Parent.status2.includes(:children).where(children: {name: 'ABC'}))

根据您的评论,我现在看到您希望记录(具有
status1
)或(具有
status2
,并且具有匹配的
子记录)

请注意,为了在
where
(如
where(子项:{name:value})
中使用关系,必须使用相关表(
连接(:子项)。其中(子项:{name:value})
。如果只使用
包含
,ActiveRecord似乎会推断连接,但据我所知,这并没有记录在案。这就是为什么
认为这两个关系不兼容的原因:一个在引用列表中有
子项,而另一个没有

如果手动将
where
子句编写为字符串,则不会更改引用列表,因此
不会将关系视为不兼容。手动编写
where
子句时,必须显式使用
联接

Parent.status1.or(Parent.status2).includes(:children).where(children: {name: 'ABC'})
您没有对最终结果或结果调用“包含”

Parent.status1.joins(:children).or(Parent.status2.joins(:children).where("children.name = 'ABC'"))
您没有对最终结果或结果调用“包含”

Parent.status1.joins(:children).or(Parent.status2.joins(:children).where("children.name = 'ABC'"))

我知道了。谢谢你的建议!我想做的是获取数据“status1或(status2和children.name=ABC)”。所以最后,我通过以下代码尝试了它。
st1\u id=Parent.status1.pulk(:id)
st2\u和\u name\u是\u abc\u id=Parent.status2.pull(:id)
conbination=Parent.where(id:st1\u id+st2\u和\u name\u是\u abc\u id)
我确实误解了您的问题。我已更新了我的答案。我可以建议您如何编写更有效的问题吗?如果您提供了一个更详细的列表,列出您期望的内容,您可能会更快地得到更好的答案。我知道了。感谢您的建议!我想做的是获取数据“状态1”或“状态1”(status2 AND children.name=ABC)”。最后,我通过以下代码进行了尝试。
st1\u id=Parent.status1.pulk(:id)
st2\u AND\u name\u是\u ABC\u id=Parent.status2.pulk(:id)
conbination=Parent.where(id:st1\u id+st2\u和_name\u是_abc\u id)
我误解了你的问题。我已经更新了我的答案。我可以建议如何写更有效的问题吗?如果你把你期望的内容列得更详细,你可能会更快得到更好的答案。