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.
Jason E. (Guest)
on 2006-01-31 18: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
Cody F. (Guest)
on 2006-01-31 19: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 E. <removed_email_address@domain.invalid> wrote:
> Thanks,
> Jason
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


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