Passing id arrays to sql


#1

Is there a way to pass an id array to find()?


#2

Hi Will,

On 19 May 2006, at 06:23, Will M. wrote:

Is there a way to pass an id array to find()?

Yes.

from http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000860

Find operates with three different retrieval approaches:

Find by id: This can either be a specific id (1), a list of ids (1,
5, 6), or an array of ids ([5, 6, 10]). If no record can be found
for all of the listed ids, then RecordNotFound will be raised.

Examples for find by id:

Person.find(1) # returns the object for ID = 1
Person.find(1, 2, 6) # returns an array for objects with IDs in
(1, 2, 6)
Person.find([7, 17]) # returns an array for objects with IDs in
(7, 17)
Person.find([1]) # returns an array for objects the object
with ID = 1

Jon

P.S. Just trying it would have been way quicker than asking. :slight_smile: