Rails 3.1 - find the counter and select

I am trying to execute the following sql statement in rails:

SELECT COUNT(downloads.title) AS total, downloads.title FROM `downloads` WHERE `downloads`.`member_id` = 60 Group by `downloads`.`title` 

I wrote this on rails as follows:

 Download.where(:member_id => id).select("COUNT(downloads.title) AS total, downloads.title").group(:title) 

If I run the query directly from the SQL server, SQL will execute correctly, but if I run the activerecord version, I will only return the header.

I thought this might be due to attr_accessible, but that didn't seem to make a difference.

any ideas?

+8
ruby-on-rails activerecord
source share
1 answer

Have you tried calling the total method on a collection object?
This information is not included in the output for the object using the to_s method, so you probably just do not see it, but there is a common value.

 downloads = Download.where(:member_id => id).select("COUNT(downloads.title) AS total, downloads.title").group(:title) downloads.first.total 
+16
source share

All Articles