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

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

@futura

Description

@futura

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions