0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

@7urkm3n Updated. Thanks.
0

There is a way, you can simply pass ruby Array to your ActiveRecord query, like this:

User.where(id: id_array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.