Reputation: 413
I have a User model. Now I have an array named id_array containing certain ids such as:
id_array=[123,145,229]
Now is there any rails way to get only those records from users table with these matching ids in id_array by using active record query?
I don't want to write a do-each loop to achieve this. I just want a single active record query which will give the result.
Please help me if there is a way. Thanks in advance!!
Upvotes: 0
Views: 31
Reputation: 3251
Passing an array for a column translates to WHERE id IN (id1, id2, id3)
in sql.
all
triggers the query and returns the results.
User.where(id: id_array).all
Upvotes: 1
Reputation: 51151
There is a way, you can simply pass ruby Array
to your ActiveRecord
query, like this:
User.where(id: id_array)
Upvotes: 0