Sort_by with via a parent models child

I am having a problem with ruby’s sort_by function. I want to sort a
list of parent model objects based on the children of the parent models.
Normally you might say sortedList = modelList.sort_by { |model|
model[‘someAttribute’] }
What I am tring to do is

sortedList = modelList.sort_by{ |model| model.children.find(:first,
order=>“price desc” ).price}

This doesn’t seem to have an effect on the list returned. Does anyone
here have any experience using the sort_by function?

I just realized that this was working. sort_by only works in one
direction so I just put this in a method and pass a sort order direction
param I if I want and ascending list I just append a .reverse! to the
end of the sorted_list.

On Thursday, July 20, 2006, at 9:57 PM, Doug Tangren wrote:

here have any experience using the sort_by function?

Posted via

Rails mailing list
[email protected]

Try this…

sortedList = modelList.sort_by {|model| {|x|

you could probably do something similar with a database query

sortedList = Child.maximum(:price, :group=>:parent_id,
:order=>‘max_price DESC’)



I just posted on this very topic today:


On 20 Jul 2006 20:25:45 -0000, Kevin O. <