【发布时间】:2026-01-18 16:20:04
【问题描述】:
我正在寻找一种在 Rails 中构建大型动态查询并将更新应用于给定 ActiveRecord 关系的方法:
例如:
my_big_set = Set.new(big_array)
to_update_relationship = my_big_set.reduce(none) do |query, values_to_check|
where('condition = ? and condition_two = ?', values_to_check[:first], values_to_check[:two]).or(query)
end
to_update_relationship.update_all(field_to_update: true)
to_update_relationship 不太大时效果很好。但如果它变得更大,那么reduce 输出在构造 Arel 查询时会触发SystemStackError: stack level too deep 错误。
有没有巧妙的解决方法? (除了拆分输入或增加 ruby 系统堆栈大小)。
谢谢
PS:使用 rails 6 和 ruby 2.7
【问题讨论】:
标签: ruby-on-rails ruby activerecord arel