Last active
December 16, 2015 07:09
-
-
Save oelmekki/5396826 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Definitely love that, thanks