meta_where
meta_where copied to clipboard
.count() not parsing MetaWhere::Function
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!
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