我试图在rails中执行以下sql语句:
SELECT COUNT(downloads.title) AS total, downloads.title FROM `downloads` WHERE `downloads`.`member_id` = 60 Group by `downloads`.`title`
我在这样的轨道中写了这个:
Download.where(:member_id => id).select("COUNT(downloads.title) AS total, downloads.title").group(:title)
如果我直接从sql server运行查询,sql会正确执行,但如果我运行activerecord版本,我只能获得标题。
我认为这可能是因为attr_accessible,但这似乎没有什么区别。
任何想法?
答案 0 :(得分:16)
您是否尝试在集合对象上调用total
方法?
使用to_s
方法的对象输出中不包含此信息,因此您可能只是看不到它,但总值是存在的。
downloads = Download.where(:member_id => id).select("COUNT(downloads.title) AS total, downloads.title").group(:title)
downloads.first.total