Named_scope and having?


#1

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.


#2

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.


#3

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.


#4

are you running rails 2.3 or higher?


#5

On 12 Feb, 13:13, MaD removed_email_address@domain.invalid 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…)


#6

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


#7

On 12 Feb, 14:18, MaD removed_email_address@domain.invalid 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.


#8

quote changelog:
2.3.0/3.0

  • Add :having as a key to find and the relevant associations.
    [miloops]

#9

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…


#10

On 12 Feb, 17:08, Gabriele T. removed_email_address@domain.invalid wrote:

Hmmmm! Even worse… named_scopes completely skip the :order key if

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