Skip to content

Instantly share code, notes, and snippets.

@oelmekki
Last active December 16, 2015 07:09
Show Gist options
  • Save oelmekki/5396826 to your computer and use it in GitHub Desktop.
Save oelmekki/5396826 to your computer and use it in GitHub Desktop.
class ActiveRecord::Base
def self.any_of( *queries )
queries = queries.map do |query|
query = where( query ) if [ String, Hash ].any? { |type| query.kind_of? type }
query = where( *query ) if query.kind_of?( Array )
query.arel.constraints.reduce( :and )
end
where( queries.reduce( :or ) )
end
end
@apneadiving
Copy link

Definitely love that, thanks

@imme5150
Copy link

This is awesome. It confused me a bit because I tried to pass in an array of hashes like this: [{name:'Joe',age:42},{name:'Frank',age:32}] you have to splat (*) your arguments to get it to work w/ the way it's written. I'm going to fork it and rewrite it w/o the splat in the argument list so you can pass in an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment