ActiveRecord::Sort
ActiveRecord::Sort
provides and easy way to accept user input and order a query by the input.
Installation
Add sunstone
to your Gemfile and run bundle
:
gem 'activerecord-sort', require: 'active_record/sort'
Or install the gem and require it:
gem install activerecord-sort
irb
Examples
ActiveRecord::Sort
supports the following cases:
Property.sort(:id).to_sql
Property.sort(:id, :name).to_sql
Property.sort(id: :desc).to_sql
Property.sort(id: {asc: :nulls_first})
Property.sort(id: {asc: :nulls_last})
It can also sort on relations:
Property.sort(addresses: :id).to_sql
Property.sort(addresses: {id: :desc}).to_sql
Property.sort(addresses: {id: {asc: :nulls_frist}}).to_sql