Ar_orderable is a project mainly written in Ruby, based on the MIT license.
ar_orderable adds a consistent way to order results at database level using ActiveRecord. Supports ordering by multiple attributes and by associated model's attributes.
h2. Introduction
ar_orderable is a small ActiveRecord addition that makes ordering of results easier and cleaner.
Supported features are:
Tested with ActiveRecord 2.3.5. Should work without hassle using any 2.3.x version of ActiveRecord. Support for ActiveRecord 3.0.x is upcoming.
Supported & tested database adapters include PostgreSQL, MySQL and SQLite3. Might work with others.
h2. Installation
Using Bundler with Rails 2.3.x add this to your Gemfile:
gem "ar_orderable", :require => "orderable"
As plugin:
./script/plugin install git://github.com/mnylen/ar_orderable.git
h2. Usage
In model:
class Game < ActiveRecord::Base
belongs_to :developer
orderable :by => [:name, :"developer.name"], :default => :name
end
In controller:
Game.order(:name) # or Game.order("name")
To order by association's attribute:
Game.order(:"developer.name") # or Game.order("developer.name")
To order in descending order:
Game.order(:"name!desc") # or Game.order("name!desc")
To order in case-sensitive manner:
Game.order(:name, :case_sensitive => true)
To order by multiple attributes:
Game.order([:"developer.name", :name]) # orders results first by developer name, then by game name
If trying to order by an attribute that is not orderable by, it will order by the default attribute:
Game.order(:updated_at) # will order by name