Reputation: 943
I want to write a Rails 5 finder query that returns objects if a field does not belong to a certain set of values or is null. I tried the below
Order
.joins(...joins1...)
.joins(...joins2...)
.left_outer_joins(:customer => :program)
.where.not(
:programs => { :number => excluded_numbers }
).or(:programs => { :number => nil})
.where(...)
but this results in a
ArgumentError: You have passed Hash object to #or. Pass an ActiveRecord::Relation object instead.
What is the proper way to form my "OR" clause?
Upvotes: 1
Views: 564
Reputation: 33460
As the error says, you must pass an ActiveRecord::Relation
, right now you're passing the { :programs => { :number => nil} }
hash.
Try with:
orders = Order.joins(...).joins(...).left_outer_joins(customer: :program)
orders.where.not(programs: { number: excluded_numbers })
.or(orders.where(programs: { number: nil }))
.where(...)
The condition for or
to work is that the two relations must be structurally compatible, so, both must include the joins and left_outer_joins you're using. That's the purpose of storing them in a local variable.
Upvotes: 1