在 where.not 中使用或子句
Use or clause within where.not
我一直在尝试将 Rails 5 应用程序升级到 Rails 6 并在此过程中删除一些原始 SQL。
我已经设法将原始 SQL LEFT JOIN 转换为 ActiveRecord 表示法。不幸的是,我只是在 where 子句中的 OR 条件下失败了。
旧代码:
::Tyre::Entry.joins(" LEFT JOIN `core_manufacturers` ON `core_manufacturers`.`id` = `tyre_entries`.`core_manufacturer_id`
LEFT JOIN `tyre_designs` ON `tyre_designs`.`id` = `tyre_entries`.`tyre_design_id`
LEFT JOIN `tyre_dimensions` ON `tyre_dimensions`.`id` = `tyre_entries`.`tyre_dimension_id`
LEFT JOIN `tyre_seasons` ON `tyre_seasons`.`id` = `tyre_entries`.`tyre_season_id`
LEFT JOIN `tyre_usages` ON `tyre_usages`.`id` = `tyre_entries`.`tyre_usage_id`
LEFT JOIN `tyre_market_segments` ON `tyre_market_segments`.`id` = `tyre_entries`.`tyre_market_segment_id`")
.where("core_manufacturers.id IS NULL OR
tyre_designs.id IS NULL OR
tyre_dimensions.id IS NULL OR
tyre_seasons.id IS NULL OR
tyre_usages.id IS NULL OR
tyre_market_segments.id IS NULL ")
到目前为止我创建的代码:
::Tyre::Entry.left_outer_joins(:manufacturer, :design, :dimension, :season, :usage, :market_segment)
.where.not(core_manufacturers: { id: nil },
tyre_designs: { id: nil },
tyre_dimensions: { id: nil },
tyre_seasons: { id: nil },
tyre_usages: { id: nil },
tyre_market_segments: { id: nil })
问题是这样where-conditions与AND链接,实际上应该是OR。
我知道以下代码片段不起作用,但有没有办法让它在这个简短的符号中起作用?
::Tyre::Entry.left_outer_joins(:manufacturer, :design, :dimension, :season, :usage, :market_segment)
.where.not(core_manufacturers: { id: nil }, or
tyre_designs: { id: nil }, or
tyre_dimensions: { id: nil }, or
tyre_seasons: { id: nil }, or
tyre_usages: { id: nil }, or
tyre_market_segments: { id: nil })
rails 做 or
的方法是这样的:
::Tyre::Entry
.left_joins(:manufacturer, :design, :dimension, :season, :usage, :market_segment)
.where(core_manufacturers: { id: nil })
.or( ::Tyre::Entry.where(tyre_designs: { id: nil }) )
.or( ::Tyre::Entry.where(tyre_dimensions: { id: nil }) )
.or( ::Tyre::Entry.where(tyre_seasons: { id: nil }) )
.or( ::Tyre::Entry.where(tyre_usages: { id: nil }) )
.or( ::Tyre::Entry.where(tyre_market_segments: { id: nil }) )
要获取“IS NULL”,请使用 where(...)
;要获得“IS NOT NULL”,请使用 where.not(...)
https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-or
你也可以使用Arel
:
::Tyre::Entry
.left_joins(:manufacturer, :design, :dimension, :season, :usage, :market_segment)
.where(CoreManufacturers.arel_table[:id].eq(nil)
.or( TyreDesign.arel_table[:id].eq(nil) )
.or( TyreDimension.arel_table[:id].eq(nil) )
.or( TyreSeason.arel_table[:id].eq(nil) )
.or( TyreUsage.arel_table[:id].eq(nil) )
.or( TyreMarketSegment.arel_table[:id].eq(nil) )
)
要获取“IS NULL”,请使用 eq(nil)
;要获得“IS NOT NULL”,请使用 not_eq(nil)
我一直在尝试将 Rails 5 应用程序升级到 Rails 6 并在此过程中删除一些原始 SQL。
我已经设法将原始 SQL LEFT JOIN 转换为 ActiveRecord 表示法。不幸的是,我只是在 where 子句中的 OR 条件下失败了。
旧代码:
::Tyre::Entry.joins(" LEFT JOIN `core_manufacturers` ON `core_manufacturers`.`id` = `tyre_entries`.`core_manufacturer_id`
LEFT JOIN `tyre_designs` ON `tyre_designs`.`id` = `tyre_entries`.`tyre_design_id`
LEFT JOIN `tyre_dimensions` ON `tyre_dimensions`.`id` = `tyre_entries`.`tyre_dimension_id`
LEFT JOIN `tyre_seasons` ON `tyre_seasons`.`id` = `tyre_entries`.`tyre_season_id`
LEFT JOIN `tyre_usages` ON `tyre_usages`.`id` = `tyre_entries`.`tyre_usage_id`
LEFT JOIN `tyre_market_segments` ON `tyre_market_segments`.`id` = `tyre_entries`.`tyre_market_segment_id`")
.where("core_manufacturers.id IS NULL OR
tyre_designs.id IS NULL OR
tyre_dimensions.id IS NULL OR
tyre_seasons.id IS NULL OR
tyre_usages.id IS NULL OR
tyre_market_segments.id IS NULL ")
到目前为止我创建的代码:
::Tyre::Entry.left_outer_joins(:manufacturer, :design, :dimension, :season, :usage, :market_segment)
.where.not(core_manufacturers: { id: nil },
tyre_designs: { id: nil },
tyre_dimensions: { id: nil },
tyre_seasons: { id: nil },
tyre_usages: { id: nil },
tyre_market_segments: { id: nil })
问题是这样where-conditions与AND链接,实际上应该是OR。
我知道以下代码片段不起作用,但有没有办法让它在这个简短的符号中起作用?
::Tyre::Entry.left_outer_joins(:manufacturer, :design, :dimension, :season, :usage, :market_segment)
.where.not(core_manufacturers: { id: nil }, or
tyre_designs: { id: nil }, or
tyre_dimensions: { id: nil }, or
tyre_seasons: { id: nil }, or
tyre_usages: { id: nil }, or
tyre_market_segments: { id: nil })
rails 做 or
的方法是这样的:
::Tyre::Entry
.left_joins(:manufacturer, :design, :dimension, :season, :usage, :market_segment)
.where(core_manufacturers: { id: nil })
.or( ::Tyre::Entry.where(tyre_designs: { id: nil }) )
.or( ::Tyre::Entry.where(tyre_dimensions: { id: nil }) )
.or( ::Tyre::Entry.where(tyre_seasons: { id: nil }) )
.or( ::Tyre::Entry.where(tyre_usages: { id: nil }) )
.or( ::Tyre::Entry.where(tyre_market_segments: { id: nil }) )
要获取“IS NULL”,请使用 where(...)
;要获得“IS NOT NULL”,请使用 where.not(...)
https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-or
你也可以使用Arel
:
::Tyre::Entry
.left_joins(:manufacturer, :design, :dimension, :season, :usage, :market_segment)
.where(CoreManufacturers.arel_table[:id].eq(nil)
.or( TyreDesign.arel_table[:id].eq(nil) )
.or( TyreDimension.arel_table[:id].eq(nil) )
.or( TyreSeason.arel_table[:id].eq(nil) )
.or( TyreUsage.arel_table[:id].eq(nil) )
.or( TyreMarketSegment.arel_table[:id].eq(nil) )
)
要获取“IS NULL”,请使用 eq(nil)
;要获得“IS NOT NULL”,请使用 not_eq(nil)