Forum: Ruby on Rails Setting a default order for a model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D2b2017098da3e2fd1cdcfbb72ecdbfb?d=identicon&s=25 Jason Edgecombe (Guest)
on 2006-01-31 17:45
(Received via mailing list)
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
2ccb2f7699267d258faaafce4ee997df?d=identicon&s=25 Cody Fauser (Guest)
on 2006-01-31 18:16
(Received via mailing list)
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:
> Thanks,
> Jason
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Cody Fauser
http://www.codyfauser.com
This topic is locked and can not be replied to.