Question

I can't find an answer for this elsewhere, so I'm asking here:

How do I "SELECT COUNT" with data_mapper?

What I've tried:

  1. MyClass.count
  2. MyClass.size
  3. MyClass.all.count
  4. MyClass.all.size

What does work is:

ids = []
MyClass.all.each do |class|
    ids << class.id
end
ids.size

But that's a bit horrible. Anyone know any better way?

Was it helpful?

Solution

Your first try was right but if you look at the documentation, count is a aggregate function and therefore you need to install/require dm-aggregates.

Aggregate functions

For the following to work, you need to have dm-aggregates required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top