NoMethodError: undefined method and for #ActiveRecord::Relation []

Viewed 1303

I'm using Rails 5. I want to write a where clause with an AND to only return results subject to two conditions. I tried this

  new_orders = Order
    .joins(:line_items => { :shipping => :vendor })
    .where.not(
      :vendors => { :id => vendor_ids }
    )
  ).and( 
    Order.joins(:line_items).where(:holding => nil)
  )

but am getting a NoMethodError: undefined method and for #ActiveRecord::Relation []. Did you mean? any?" error.

1 Answers

There is no and method available for an ActiveRecord Relation. There is only an or method available.

For your query, I think this should also work

new_orders = Order.joins(:line_items => { :shipping => :vendor })
                  .where.not(:vendors => { :id => vendor_ids })
                  .where(:holding => nil)
Related