Forum: Ruby on Rails Ordering Results returned by has_many relationship.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6e03895e5abb46f3daf4aafad3b98d5c?d=identicon&s=25 Randal Santia (Guest)
on 2006-05-02 22:28
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
6e03895e5abb46f3daf4aafad3b98d5c?d=identicon&s=25 Randal Santia (Guest)
on 2006-05-02 22:47
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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-05-03 19:02
(Received via mailing list)
On May 2, 2006, at 04:28 PM, Randal Santia wrote:
>
> 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
This topic is locked and can not be replied to.