Forum: Ruby on Rails :order question

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.
A1af940a886ef63916612429a9d9bb19?d=identicon&s=25 esdevs (Guest)
on 2009-03-11 23:31
(Received via mailing list)
ok so I am trying to do something like the below to order by :joins (I
know the below won't work)...the only solution that I can fathom is
using something like find_by_sql - but I certainly would prefer a
simpler method...any suggestions?

User.find(:all, :joins => :deals, :group => 'rev', :conditions =>
['saledate >= ? AND saledate <= ?', Time.now.at_beginning_of_month,
Time.now.at_end_of_month], :order => "deals.rev.sum DESC")

Thanks

Sean
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-12 00:06
(Received via mailing list)
On Mar 11, 10:30 pm, esdevs <seanpdev...@gmail.com> wrote:
> ok so I am trying to do something like the below to order by :joins (I
> know the below won't work)...the only solution that I can fathom is
> using something like find_by_sql - but I certainly would prefer a
> simpler method...any suggestions?
>
> User.find(:all, :joins => :deals, :group => 'rev', :conditions =>
> ['saledate >= ? AND saledate <= ?', Time.now.at_beginning_of_month,
> Time.now.at_end_of_month], :order => "deals.rev.sum DESC")
>

I'm not entirely sure what you're doing - :order has to be a valid sql
order clause, so order by any column from any of your joined tables,
or an sql expression is fine (:eg :order => "SUM(something)" should be
ok, although you might need the expression in your order clause to be
part of the select clause too).

Fred
A1af940a886ef63916612429a9d9bb19?d=identicon&s=25 esdevs (Guest)
on 2009-03-12 00:16
(Received via mailing list)
so to clarify - I am trying to order by the sum of the items in a
column in a joined table between a certain date range.

On Mar 11, 7:06 pm, Frederick Cheung <frederick.che...@gmail.com>
This topic is locked and can not be replied to.