Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Default Order

Default Order

Author : Antonin AMAND Email : aamand@gwikzone.org License : Ruby MIT

The default_order plugin provides a ‘order_by’ method to be called in your models.

ex :

class Contact < ActiveRecord::Base order_by :fields => [‘first_name’, ‘last_name’], :mode => :desc

end

  1. mode can be either a Symbol or a String order_by :fields => [‘first_name’, ‘last_name’], :mode => :desc order_by :fields => [‘first_name’, ‘last_name’], :mode => “DESC”

mode default to ASC

For compatibility with previous version you can use the string form but it is not recommended

order_by “contacts.first_name, contacts.last_name DESC

the order_by method will append the given argument to :order Hash key of ActiveRecord::Base.find method IF IT HAS NOT BEEN PROVIDED.

ex :

Contact.find(:all, :conditions => “last_name LIKEtoto’”)
  1. will act as if you had written this
Contact.find(:all, :order => “last_name, first_name”, :conditions => “last_name LIKEtoto’”)
  1. but if you provide :order it will override the default order

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly