How can I sort children objects with RoR / Ruby?

LL0rd
LL0rd used Ask the Experts™
on
Hi,

I want to sort the children of an object, how can I do that? Example:
The Object "Hotel" has many "Rooms". A room has a real room number.

In my view File, I iterate through the hotel.rooms, but how can I get the right order of the rooms by it's room number?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
you can sort the objects in the controller
    @claims = Claim.find(:all)
    @claims = @claims.sort_by {|c| [c.village.K, c.village.x, c.village.y]}
or you can do the same in the view if you want to

sor_by is what you need
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
sort_by instead of sor_by of course
translated to your example this would be

@rooms = Rooms.find(:all)
@rooms = @rooms.sort_by {|r| [r.room_number]}
Actually, sorting them after the fact would be rather inefficient.

You can pass :order to the find() method:

You can also use named_scopes to apply sorting information.  They're super handy, so you might want to check them out.
@rooms = Room.find(:order => 'number')

# or, for a hotel
@rooms = Room.find_by_hotel_id(@hotel.id, :order => 'number')

# or, you can specify a default in your model
#  hotel.rb
has_many :rooms, :order => 'number'

# then the association will return them sorted
@hotel = Hotel.first
@hotel.rooms # sorted by order

Open in new window

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Andrew DoadesIT Manager

Commented:
Hi

Im actually trying to do the same kind of thing, but I've got
categories and category_downloads, and within the view I want to order by category_downloads count

I'm stuck as to how to get it, I've got the count category.category_documents.count but them how to I order by count in the view?

Sorry to but-in on this topic!

Andrew
doades:  add a counter_cache and order by that. Check out the documentation for belongs_to()

Author

Commented:
@wesgarrison

Thx, that was exactly the solution, I was looking for:
has_many :rooms, :order => 'number'
Andrew DoadesIT Manager

Commented:
And mine, counter_cache does exactly what I waned.

Cheers,
Andrew

Author

Commented:
Sorry, just another question. At the moment, I use this statement in my model:
has_many :rooms, :order => 'number'

So the rooms are statically sorted by room number. Is it possible, to define the sort criteria a little bit dynamically? Yeah well, I can define a method for each order, like that:

def roomy_sorted_by_used_times
  Room.find_by_hotel_id(@hotel.id, :order => 'used_times')
end

Or is there a more "Rails Way"?
named_scope is what you want, probably.

http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002227&name=named_scope
http://railscasts.com/episodes?search=named_scope

If you want to do a COUNT() on another field or something, you can override :select and :joins in your find() method to pull in the appropriate tables.

Author

Commented:
Thx again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial