named_scope and having?

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.

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.

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.

are you running rails 2.3 or higher?

no, I'm not, I'm using rails 2.2.2... (the current stable, I believe...)

i may be wrongv but i think :having in scopes is new to rails 2.3

quote changelog: *2.3.0/3.0* * Add :having as a key to find and the relevant associations. [miloops]

Thank you.. :slight_smile: 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! :slight_smile:

G.

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..

Obviously referencing the :group key, not the :order one...