E
E
eldar_web2015-08-25 13:52:01
Ruby on Rails
eldar_web, 2015-08-25 13:52:01

How to show only the count when querying .group().count in Rails?

Subscriber.all.count => 7
Subscriber.group(:id).count => {12=>1, 13=>1}
Why does group give me a hash, not an absolute number ???
How to decide?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
Evgeny Tkachenko, 2015-08-25
@eldar_web

And so?

Subscriber.count(:all, :group => 'id')

P
Pavel Kononenko, 2015-08-25
@premas

What does it mean to decide? In the code , the
absolute quantity is returned, and in the code
, Grouped by the id field.
What other result would you like to see?

T
thepry, 2015-08-25
@thepry

Subscriber.group(:id).count => {12=>1, 13=>1}

There is no point in this. Their id seems to be unique.
Then, that group exists for this, to group records by one field, aggregating others.

V
vsuhachev, 2015-08-25
@vsuhachev

I looked at the answer about count with parameters and it became interesting that I don’t know about count. It turned out (in 4 rails) that
a rather meaningless expression and is equivalent to Subscriber.count. To be convinced of this, it is enough to read the raw , by the way, in 5 rails, the second parameter from count has already been cut out ...

D
Dmitry Amelchenko, 2015-08-28
@i1677960

Subscriber.pluck(:id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question