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 http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Try this…

sortedList = modelList.sort_by {|model| model.children.map {|x|
x.price}.max}

you could probably do something similar with a database query

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

_Kevin
www.sciwerks.com

Doug,

I just posted on this very topic today:

http://javathehutt.blogspot.com/2006/07/rails-realities-part-16-finders-and.html

-Michael

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs