2

I have this error message Relation passed to #or must be structurally compatible. Incompatible values: [:joins]

in my user model: has_many :orders

in my order model: belongs_to :user, optional: true

How I am supposed to write my query to have either the users' names and the order id in the same search input?

def filter_orders
  return if params[:query].blank?
  @orders = Order.joins(:user).where('lower(users.first_name) LIKE ?', "%#{params[:query][:keyword]}%")
  .or(Order.joins(:user).where('lower(users.last_name) LIKE ?', "%#{params[:query][:keyword]}%"))
  .or(Order.where(id: "#{params[:query][:keyword]}.to_i"))
end
johan
  • 691
  • 4
  • 19

1 Answers1

1

It sound like this is a know issue with .or. Try using SQL or you can override .or as seen in this answer: https://stackoverflow.com/a/40742512/10987825

Noah
  • 550
  • 2
  • 7