# Sort\_by with via a parent models child

**URL:** <https://www.ruby-forum.com/t/sort-by-with-via-a-parent-models-child/99191>\
**Category:** Rails\
**Created:** [July 20, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/sort-by-with-via-a-parent-models-child/99191 "2006-07-20T21:57:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruby-doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@ruby-doug](https://www.ruby-forum.com/u/ruby-doug)\
**Post date:** [July 20, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/sort-by-with-via-a-parent-models-child/99191/1 "2006-07-20T21:57:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ruby-doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@ruby-doug](https://www.ruby-forum.com/u/ruby-doug)\
**Post date:** [July 20, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/sort-by-with-via-a-parent-models-child/99191/2 "2006-07-20T22:22:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ruby-doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@ruby-doug](https://www.ruby-forum.com/u/ruby-doug)\
**Post date:** [July 20, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/sort-by-with-via-a-parent-models-child/99191/3 "2006-07-20T22:27:11Z")

</div>

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/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![ruby-doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@ruby-doug](https://www.ruby-forum.com/u/ruby-doug)\
**Post date:** [July 20, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/sort-by-with-via-a-parent-models-child/99191/4 "2006-07-20T22:30:35Z")

</div>

Doug,

I just posted on this very topic today:

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

-Michael

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