Hi, I''m using a room model throughout my code. I keep doing the following: @rooms = Room.find(:all, :order => ''buildings.name, room_number'', :include => :building) I have to copy this into several of my controllers and I feel that I''m violating the DRY principle. Can I set this as the default order in my model? If so, how would I do that? Thanks, Jason
> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > Jason Edgecombe > Sent: Tuesday, January 31, 2006 9:44 AM > To: rails@lists.rubyonrails.org > Subject: [Rails] Setting a default order for a model? > > > Hi, > > I''m using a room model throughout my code. I keep doing the following: > @rooms = Room.find(:all, :order => ''buildings.name, room_number'', > :include => :building) > > I have to copy this into several of my controllers and I feel > that I''m > violating the DRY principle. Can I set this as the default > order in my > model? If so, how would I do that? > > Thanks, > JasonI''d recommend defining a custom method: class Room < ActiveRecord::Base def self.find_ordered(first=''name'', second=''room_number'') self.find(:all, :order => "buildings.#{first}, #{second}", :include => :building) end end Then just do: @rooms = Room.find_ordered Regards, Dan
Jason, You could add a static method to your Room model: def self.find_all_include_building find(:all, :order => ''buildings.name, room_number'', :include => :building) end Then just: @rooms = Room.find_all_include_building On 1/31/06, Jason Edgecombe <jedgecombe@carolina.rr.com> wrote:> Hi, > > I''m using a room model throughout my code. I keep doing the following: > @rooms = Room.find(:all, :order => ''buildings.name, room_number'', > :include => :building) > > I have to copy this into several of my controllers and I feel that I''m > violating the DRY principle. Can I set this as the default order in my > model? If so, how would I do that? > > Thanks, > Jason > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cody Fauser http://www.codyfauser.com