Project

hurray

0.0
No release in over 3 years
Low commit activity in last 3 years
Simple ActiveRecord extension for working with database arrays fields
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

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