Ruby on Rails : Find with default order 1

Posted by Antonin AMAND Tue, 05 Sep 2006 12:56:00 GMT

I’ve found a solution (I am propably not the first ;-) ) for default ordering on ActiveRecord:Base children. The tip is to override the find method :


class Contact < ActiveRecord:Base

  def Contact.find(*args)
    if args[1] 
      args[1][:order] = "last_name, first_name" if args[1].is_a?(Hash) && !args[1][:order]
    else
      args[1] = {:order => "last_name, first_name"}
    end
    super(*args)
  end

end

If you have something cleaner please comment this post.

Trackbacks

Use the following link to trackback from your own site:
http://blog.gwikzone.org/trackbacks?article_id=ruby-on-rails-find-with-default-order&day=05&month=09&year=2006

Comments

Leave a comment

  1. lomakin over 2 years later:

    Why you use so strange construction "Contact.find". I think "self.find" is more cleaner and right way.

Comments