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!
Using rails 3.x:
Foo.find_all_by_name(['me','you','him']);
Or using rails 4.x
Foo.where(name: ['me','you','him'])
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'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.