Reputation: 2888
I have following sql query in Rails 3 syntax:
variab = Model.joins(:photo).where('photos.user_id = ?', user).group('model.photo_id').count
In the Models
table I have these data:
+----+----------+----------+---------------------+----------+
| id | photo_id | created_at | updated_at |
+----+----------+----------+---------------------+----------+
| 31 | 210 | 2011-09-16 14:38:22 | 2011-09-16 14:38:22 |
| 32 | 210 | 2011-09-16 14:38:22 | 2011-09-16 14:38:22 |
| 33 | 211 | 2011-09-16 15:03:26 | 2011-09-16 15:03:26 |
+----+----------+----------+---------------------+----------+
And I try to get the value 2 - I need to get count of unique photo_id data. But if I will use the sql query above, so the output is ** 21022111** - I don't understand, what this number mean... I would assume the output should be 2, but I don't understand, why not...
If anyone could help me with this problem, I would be really glad... Thank you so much
Upvotes: 0
Views: 98
Reputation: 15736
Active Record is returning the photo_id/count pairs, i.e. photo_id 210 count 2, photo_id 211 count 1.
If you want to count the number of distinct photos for a given use then try:
Model.select(:photo_id).where(:user_id => user.id).count(:distinct => true)
Upvotes: 2