Forum: Ruby on Rails named_scope and having?

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.
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-12 12:58
(Received via mailing list)
Hi all,
is just my fault.. or I can't write an having caluse for a group in a
named scope? °_°

i.e.

named_scope :last_year_best_buyers, :joins => :invoices, :conditions
=> {"invoices.closed" => true, "invoices.issued_on" =>
1.years.ago..Date.today}, :group => "customers.id", :having => "SUM
(invoices.taxable_income) > 15000"

Calling Customer.last_year_best_buyers, leads to a:

ArgumentError: Unknown key(s): having

How can I specify an having clause (the only way I know to filter
results using aggregate functions, ain't it)?

thanks
G.
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-12 13:01
(Received via mailing list)
Well... I always can specify it on the group clause.. but it doesn't
look so clean to me (esp. since the find method has the having
clause... °_°)

named_scope :last_year_best_buyers, :joins => :invoices, :conditions
=> {"invoices.closed" => true, "invoices.issued_on" =>
1.years.ago..Date.today}, :group => "customers.id HAVING SUM
(invoices.taxable_income) > 15000"

G.
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-12 13:04
(Received via mailing list)
Another problem with having to specify the HAVING in the group key..
is that it looks like it would lead to errors when sequencing multiple
named_scopes, i.e.:

Customer.last_year_best_buyers.with_active_account

But I have to investigate more on this...

G.
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-12 13:14
(Received via mailing list)
are you running rails 2.3 or higher?
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-12 14:05
(Received via mailing list)
On 12 Feb, 13:13, MaD <mayer.domi...@gmail.com> wrote:
> are you running rails 2.3 or higher?

no, I'm not, I'm using rails 2.2.2... (the current stable, I
believe...)
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-12 14:18
(Received via mailing list)
i may be wrongv but i think :having in scopes is new to rails 2.3
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-12 14:19
(Received via mailing list)
quote changelog:
*2.3.0/3.0*
* Add :having as a key to find and the relevant associations.
[miloops]
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-12 14:45
(Received via mailing list)
On 12 Feb, 14:18, MaD <mayer.domi...@gmail.com> wrote:
> quote changelog:
> *2.3.0/3.0*
> * Add :having as a key to find and the relevant associations.

Thank you.. ^_^ I was guessing about it since your first post.. now I
just hobe rail 2.3 won't brake too much things, it's never good to
have to switch framework version in the middle of an app development!
^_^

G.
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-12 17:09
(Received via mailing list)
Hmmmm! Even worse... named_scopes completely skip the :order key if
there's a HAVING clause.. °_° I thought I could have used wht I
proposed at 13:00, but the named scope didn't used the group key at
all to find the resultset... sic...
Quite annoying, I'm really hoping 2.3 won't break too much
compatibility with 2.2.2 code..
31fae7b624e38edb7b78753b71bacff3?d=identicon&s=25 Gabriele Tassoni (Guest)
on 2009-02-12 17:14
(Received via mailing list)
On 12 Feb, 17:08, Gabriele Tassoni <gabriele.tass...@gmail.com> wrote:
> Hmmmm! Even worse... named_scopes completely skip the :order key if

Obviously referencing the :group key, not the :order one...
This topic is locked and can not be replied to.