ActiveRecord find ordering
ActiveRecord find can return multiple records. Unfortunately, database defaults conspire to
potentially surprise us later.
user_ids = [123, 37, 42]
# works!
users = User.find(user_ids)
users = company.users.find(user_ids)
# not guaranteed to work
users = company.users.where(id: user_ids)
The thing to be careful for here is some ways that aren’t guaranteed to work may appear to work
due to natural ordering in the database. That is, due to index layout or other hijinks you have no
control over as a Rails developer, you could pass IDs out of order, like user_ids above, and
they’d come back in the right order by happenstance but not for every single case. AR find with an
array of IDs is defined in the API docs to preserve order. So if it doesn’t, it’s a bug in AR and
not your code.