Search code examples
ruby-on-railspostgresqlactiverecordruby-on-rails-2

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!!


Solution

  • 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