Randal Santia
2006-May-02 20:28 UTC
[Rails] Ordering Results returned by has_many relationship.
Noob Question 31,265,232 if I''m searching on an object, say order, that has many "order_lines" and I want to display order lines by Quantity ( an attribute of the order_lines ) descending how could I do that without having to do a find() with :order, but something like; Order.order_lines.each do |ol| <!-- code to diplay the line --> how can I determine the field and the way I order the results? Thanks in advance, Randal the Newbie -- Posted via http://www.ruby-forum.com/.
Brian Hughes
2006-May-02 20:37 UTC
[Rails] Ordering Results returned by has_many relationship.
On May 2, 2006, at 04:28 PM, Randal Santia wrote:> Noob Question 31,265,232 > > if I''m searching on an object, say order, that has many "order_lines" > and I want to display order lines by Quantity ( an attribute of the > order_lines ) descending > how could I do that without having to do a find() with :order, but > something like; > > Order.order_lines.each do |ol| > <!-- code to diplay the line --> > > how can I determine the field and the way I order the results?class Order < ActiveRecord::Base has_many :order_lines, :order => "order_lines.quantity" end More info: <http://rails.rubyonrails.org/classes/ActiveRecord/ Associations/ClassMethods.html#M000530> -Brian
Campano, David
2006-May-02 20:41 UTC
[Rails] Ordering Results returned by has_many relationship.
Since Order.order_lines is really just an array, then you can use the sort feature of an array like this: sorted = Order.order_lines.sort {|x,y| y.quantity <=> x.quantity } Then do sorted.each... Hope this helps! -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org]On Behalf Of Randal Santia Sent: Tuesday, May 02, 2006 4:28 PM To: rails@lists.rubyonrails.org Subject: [Rails] Ordering Results returned by has_many relationship. Noob Question 31,265,232 if I''m searching on an object, say order, that has many "order_lines" and I want to display order lines by Quantity ( an attribute of the order_lines ) descending how could I do that without having to do a find() with :order, but something like; Order.order_lines.each do |ol| <!-- code to diplay the line --> how can I determine the field and the way I order the results? Thanks in advance, Randal the Newbie -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails Please see the following link for the BlueCross BlueShield of Tennessee E-mail disclaimer: http://www.bcbst.com/email_disclaimer.shtm
Randal Santia
2006-May-02 20:47 UTC
[Rails] Re: Ordering Results returned by has_many relationship.
Nevermind, I''m just a little slow. I was puttin in the ORDER class, how to order the order_lines, and didn''t realise I could do has_many :order_lines, :order => "quantity DESC" problem solved, and now archieved for future noobs :) Randal Santia wrote:> Noob Question 31,265,232 > > if I''m searching on an object, say order, that has many "order_lines" > and I want to display order lines by Quantity ( an attribute of the > order_lines ) descending > how could I do that without having to do a find() with :order, but > something like; > > Order.order_lines.each do |ol| > <!-- code to diplay the line --> > > how can I determine the field and the way I order the results? > > Thanks in advance, > Randal the Newbie-- Posted via http://www.ruby-forum.com/.