gpt4 book ai didi

ruby-on-rails - 如何在 Rails 3 中将两个事件记录关系与 OR 条件合并并返回结果也是一个事件关系而不是数组?

转载 作者:太空宇宙 更新时间:2023-11-03 16:20:37 24 4
gpt4 key购买 nike

我有两个关联,例如 surgical_diseases 和 eye_disease。我想获得这两个事件关系的 Ored 结果。但是下面的代码给了我一个数组。

 has_many :surgical_diseases
has_many :eye_disease

scope :all_disease ->(name) { joins(:surgical_diseases).where('surgical_diseases.name IN (?)') | joins(:eye_disease).where('eye_disease.name IN (?)') }

我见过 active-record-union gem,但它只适用于 active-record 4。我目前使用的是 rails 3.2,所以无法使用它。

我还看到此功能将随带有 dhh 的 commit 的 rails5 一起提供。 .但不确定我现在如何使用 rail3 解决这个问题。

我已尽力理解我的问题。如果需要任何其他信息,请告诉我。提前致谢!

最佳答案

您可能需要使用 find_by_sql 获取 ID,然后找到这些 ID 以获取 ActiveRecord::Relation。

scope :all_disease ->(name) {  
ids = YourTable.find_by_sql <<-SQL
SELECT your_table.id FROM your_table INNER JOIN surgical_diseases sd ON sd.your_table_id=your_table.id WHERE sd.name IN (#{name})
UNION
SELECT your_table.id FROM your_table INNER JOIN eye_diseases ed ON ed.your_table_id=your_table.id WHERE ed.name IN (#{name})
SQL

YourTable.where(id: ids)
}

关于ruby-on-rails - 如何在 Rails 3 中将两个事件记录关系与 OR 条件合并并返回结果也是一个事件关系而不是数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34103032/

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