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? -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
On Thursday, July 20, 2006, at 9:57 PM, Doug Tangren wrote:>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? > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsTry 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.
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 Olbrich < devlists-rubyonrails@devlists.com> wrote:> > > On Thursday, July 20, 2006, at 9:57 PM, Doug Tangren wrote: > >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? > > > >-- > >Posted via http://www.ruby-forum.com/. > >_______________________________________________ > >Rails mailing list > >Rails@lists.rubyonrails.org > >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 > > -- > Posted with http://DevLists.com. Sign up and save your mailbox. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060720/396ca2ac/attachment.html