Order ActiveRecord collections specified way.
Add following line to Gemfile
gem 'hurray'
Get records ordered according to the specified with array column order.
User.where(id: [4,2,6])
#=> [#<User id: 2 ...>, #<User id: 4 ...>, #<User id: 6 ...>]
If you want to get User::ActiveRecord_Relation with specified order use ordered_with(hash)
:
User.ordered_with(id: [4,2,6])
#=> User::ActiveRecord_Relation [#<User id: 4 ...>, #<User id: 2 ...>, #<User id: 6 ...>, #<User id: 1 ...>, ...]
If you want to get only these records you should use it following way:
User.where(id: [4,2,6]).ordered_with(id: [4,2,6])
#=> User::ActiveRecord_Relation [#<User id: 4 ...>, #<User id: 2 ...>, #<User id: 6 ...>, #<User id: 1 ...>]
You can use this method with multiple fields:
User.ordered_with(name: %w(John), id: [4,2,6])
#=> User::ActiveRecord_Relation [#<User id: 6, name: 'John' ...>, #<User id: 4 ...>, #<User id: 2 ...>, ...]
or with joined tables:
User.joins(:freinds).ordered_with(friends: { id: [5,2,7] })