gpt4 book ai didi

mysql - 复杂的 AR 查询,其中子句一列等于另一列的值?

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:44 27 4
gpt4 key购买 nike

我已经在下面编写了 AR 查询,但是 where 子句有一个问题。在 Payout 模型上有一个 comp_builder_id 并且在 ContractLevel 上也有一个 comp_builder_id

我想在此查询中添加一个 where 子句以获取数据,其中 Payout 模型的 comp_builder_id 等于 ContractLevel 模型的 comp_builder_id

payouts = Payout.includes(:comp_builder => { 
:contract_levels => {
:transmittal => [
{:appointment_hierarchies => :child},
:appointment_cases
]
}
})
.includes(:product)
.includes(:payor)
.where(products: { :id => self.product.id })
.where(appointment_cases: { :case_id => self.id })
.where(transmittals: { :id => appointment_case.transmittal_id })
.where(contract_levels: { :transmittal_id => appointment_case.transmittal_id,
:appointment_id => Transmittal.find(appointment_case.transmittal_id).low,
:comp_builder_id => # ? payout's comp builder id ? })

如何在 Active Record 中做到这一点?在最后几个 where 子句中,我引用了不同的变量,这些可以忽略,因为这个查询只是一个片段。

最佳答案

您可以将字符串传递到 where 语句中:

User.include(:parent).where('users.parent_id = parents.id')

基本上,如果您知道您希望 where 语句在原始 SQL 中看起来像什么,那么您可以将它作为字符串传递到 where 语句中。

这是我所知道的在 ActiveRecord 中创建复杂 where 语句的唯一方法。

请务必记住,您需要引用列名称,而不是模型名称。

关于mysql - 复杂的 AR 查询,其中子句一列等于另一列的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30088761/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com