Tuesday, 10 September 2013

Rails 4: how to use OR between conditions on find methods

Rails 4: how to use OR between conditions on find methods

I want to find objects with something like this:
conditions = {first_name: @search OR last_name: @search}
Stuff.where( conditions )
Obviously, this syntax is invalid, but it's the easiest way I can think of
to show what I want to do. I want to use the cleaner hash syntax with
complex/compound conditions.
I know you can just write it out by hand with "pure string conditions"
like this Stuff.where("first_name=#{@search} OR last_name=#{@search}")
...but this is NOT what I want to know.
Update It looks like you can do OR with an array like this: Stuff.where(
some_column: ["option1", "option2"]). This is quite useful to know, but it
doesn't solve my problem because I need the OR to apply to different
key=value pairs... key=value OR key=value not key=value OR value.

No comments:

Post a Comment