Reputation: 3272
In my Ruby on Rails application, I want to print a count result after I grouped my records in database and I get something like that :
{1=>6}
In my database there is 6 records, all with the same user_id
.
Here is my code :
Whatever.group(:user_id).count(:user_id)
I just want to print 1 how to do this. I tried with distinct
and uniq
without any success...
Upvotes: 0
Views: 309
Reputation: 211560
If you just need to compact that down to a useful result:
Whatever.group(:user_id).count.keys.join(',')
This will handle the case where you have more than one user in the result set.
The count(:user_id)
part is redundant unless you're counting based on other conditions. Just use count
instead.
Upvotes: 1
Reputation: 11234
Here is an example
('a'..'b').group_by { |i| i * 2 } #=> {"aa"=>["a"], "bb"=>["b"], "cc"=>["c"]}
('a'..'c').group_by { |i| i * 2 }.keys #=> ["aa", "bb", "cc"]
('a'..'c').group_by { |i| i * 2 }.keys[0] #=> "aa"
Upvotes: 0