Skip to content

rkotov93/hurray

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 

Repository files navigation

Hurray

Order ActiveRecord collections specified way.

Installation

Add following line to Gemfile

gem 'hurray'

ordered_with

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] })

About

Order ActiveRecord collections specified way

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages