Reputation: 3957
I need to find multiple models in my db by a unique identifier that is not the id. Is there a way to do this in rails? Basically, I'm looking for the analog of:
Foo.find([1,2,3])
for the by_attribute helpers:
Foo.find_by_name(['me','you','him'])
The issue I'm having now is that the find_by_name
helper is appending LIMIT 1
to the query it creates so I'm just getting one object back rather than an array of all of the objects I'm looking for.
Thanks!
Upvotes: 1
Views: 1907
Reputation: 1569
Using rails 3.x:
Foo.find_all_by_name(['me','you','him']);
Or using rails 4.x
Foo.where(name: ['me','you','him'])
Upvotes: 4
Reputation: 3957
As usual, I figured this out as soon as I'd written up the question for Stack Overflow :)
where
supports a list of values and will return all objects that match. So, to find all Foos
by name:
Foo.where(name: ['me','you','him'])
Upvotes: 2