60

So I have an array of user ids. Is there a way in rails console to query all of these user's with the array

something like

ids = [1, 2, 3, 4]

users = User.find(ids)

and have it return all 4 users?

Seth
  • 10,198
  • 10
  • 45
  • 68
  • 2
    It does work. You can pass an array of IDs to the model's find method and, provided they are all existing IDs, you will get back an array of user's for those IDs. – DiegoSalazar Jan 29 '14 at 15:52
  • Didn't realized this worked... haha. Should have tested it. – Seth Jan 29 '14 at 16:19

6 Answers6

138

For an array, you can use one of these:

# Will raise exception if any value not found
User.find( [1,3,5] )

# Will not raise an exception
User.find_all_by_id( [1,3,5] ) # Rails 3
User.where(id: [1,3,5])        # Rails 4

If you happen to be using a range, you can use these:

# Will raise exception if any value not found
User.find((1..4).to_a)   #same as User.find([1,2,3,4])

# Will not raise an exception
User.find_all_by_id(1..4)  # Rails 3
User.where(id: 1..4)       # Rails 4

As @diego.greyrobot notes in a comment, a range causes a SQL BETWEEN clause, whereas an array causes a SQL IN clause.

Don't use User.find_by_id() -- It will only return one record, no matter how may IDs you pass in.

Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98
  • 1
    My actual array isn't from 1 - 4. The ids are sporadic for example 122, 323, 451. Can I still achieve that? – Seth Jan 29 '14 at 15:48
  • 3
    There's a few inaccuracies here: User.find((1..4).to_a) will make it work. And: 1..4 creates a Range not an Array. – DiegoSalazar Jan 29 '14 at 15:54
  • 2
    Yes, it's important to note that passing in a range (User.find_all_by_id(1..4)) will execute a sql BETWEEN clause, whereas passing in an array will do a sql IN statement. – DiegoSalazar Jan 29 '14 at 16:48
  • Edited to add Rails 4 preferred versions. – Grant Birchmeier Mar 19 '15 at 21:53
  • Note: Duplication and order will be ignored. I use a hash to preserve both: `ids = [3, 3, 1, 0, 3]; Model.find(ids).map(:id) == [0, 1, 3]; h = Model.find(ids.uniq).map{|o| [o.id, o]}.to_h; h.values_at(ids).map(:id) == [3, 3, 1, 0, 3]` – Kanat Bolazar Oct 29 '15 at 20:19
  • Does `find` vs `where` result in the same sql query? – trigun0x2 Nov 08 '15 at 19:46
  • 1
    .find returns `Array`. Whereas .where returns `ActiveRecord::Relation` – Neeraj Aug 03 '21 at 07:41
17

you can use User.where(id: ids)

Nitin Jain
  • 3,053
  • 2
  • 24
  • 36
4

Use splash operator:

ids = [1, 2, 3, 4]

users = User.find(*ids)

Note that it will raise an exception if it fails to find any of the users.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
4

This is work for me...

ids = [1, 2, 3, 4]

users = User.find(ids)

users = User.find(*ids)

users = User.find_all_by_id(ids)

All are working..

Dheer
  • 773
  • 1
  • 6
  • 16
2

What you're doing is supposed to work when all the ids exist.

The reason you might be seeing an exception is because at least one of those ids does not exist in the database.

Instead, you want to use find_all_by_id if you don't want to get an exception:

User.find_all_by_id([1, 2, 3, 4])

# Does the following sql:    
User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IN (1, 2, 3, 4)
Abdo
  • 13,549
  • 10
  • 79
  • 98
2

you can use

   Users.where({ id: [1,2,3 ,4]})
 # SELECT * FROM users WHERE id IN (1,2,3,4)
Tarek Saied
  • 6,482
  • 20
  • 67
  • 111
  • I would encourage you to expand on this answer to provide some explanation. An answer that is all code doesn't really provide a lot of information to other users that are unfamiliar with the concepts in the question. – psubsee2003 Sep 22 '14 at 22:50