Abhradip
Abhradip

Reputation: 413

Issue regarding fetching some records from table in rails

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

Answers (2)

razvans
razvans

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

Marek Lipka
Marek Lipka

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

Related Questions