Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

.count() not parsing MetaWhere::Function #29

Open
futura opened this issue Jun 10, 2011 · 1 comment
Open

.count() not parsing MetaWhere::Function #29

futura opened this issue Jun 10, 2011 · 1 comment

Comments

@futura
Copy link

futura commented Jun 10, 2011

First off, I love, love, love MetaWhere!

Second, I'm trying to return a count with groupings and SQL functions:

Client.select(:year[:date_of_birth].as('age')).group(:county, :age)

... works great. But adding .count to the end:

Client.select(:year[:date_of_birth].as('age')).group(:county, :age).count
SELECT COUNT(#<MetaWhere::Function:0x106fd0230>) AS count_metawhere_function_0x106fd0230, county AS county, age AS age FROM `clients` GROUP BY county,age

I've worked around it by using a COUNT() sql function in the select, but it's sloppier and forces me also to select the other fields and to do a map:

Client.select([
        :count[Client.arel_table[:client_id]].as(:client_count),
        :county,
        :year[:date_of_birth].as('age')
      ]).
      group(:county, :age).map {|c| [c.county, c.age, c.client_count]}

This works; I'm just wondering if I ran into a bug or I'm not using it right. Thanks!

@futura
Copy link
Author

futura commented Jun 10, 2011

Addendum: .count() works if I'm not performing the :year function. So this works:

Client.group(:county, :gender).count
SELECT COUNT(*) AS count_all, county AS county, gender AS gender FROM `clients` WHERE `clients`.`abstract` = 0 AND `clients`.`active` = 1 AND `clients`.`referral_date` <= '2011-06-30' AND (`clients`.`discharge_date` >= '2011-04-01' OR `clients`.`discharge_date` IS NULL) GROUP BY county,gender ORDER BY last, first

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant