Rails 运行 AND 在 where 子句中使用连接进行查询
Rails run AND query in where clause with joins
目前我有以下查询
Policy.joins(policy_coverages: :cpt_code).where(policy_coverages: { cpt_codes: { code: code_array } })
此查询返回所有政策覆盖范围为数组中 cpt_codes 之一的政策。它正在执行 OR
查询。
我如何编写一个查询,使 returns 只有具有 policy_coverages 的策略以及数组 code_array
中的所有 cpt_codes
对记录进行分组并使用必须在每个组(每个策略)上设置条件:
Policy.group(:id)
.joins(policy_coverages: :cpt_code)
.where(policy_coverages: { cpt_codes: { code: code_array } })
.having(CptCode.arel_table[Arel.star].count.gteq(code_array.length))
如果您只想将记录与这些策略代码匹配,而不使用其他策略代码,请使用 .eq
而不是 .gteq
。
目前我有以下查询
Policy.joins(policy_coverages: :cpt_code).where(policy_coverages: { cpt_codes: { code: code_array } })
此查询返回所有政策覆盖范围为数组中 cpt_codes 之一的政策。它正在执行 OR
查询。
我如何编写一个查询,使 returns 只有具有 policy_coverages 的策略以及数组 code_array
对记录进行分组并使用必须在每个组(每个策略)上设置条件:
Policy.group(:id)
.joins(policy_coverages: :cpt_code)
.where(policy_coverages: { cpt_codes: { code: code_array } })
.having(CptCode.arel_table[Arel.star].count.gteq(code_array.length))
如果您只想将记录与这些策略代码匹配,而不使用其他策略代码,请使用 .eq
而不是 .gteq
。