Rails的3.1 - 发现使用count并选择以 [英] Rails 3.1 - find using count and select as

查看:207
本文介绍了Rails的3.1 - 发现使用count并选择以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的钢轨下面的SQL语句:

I am trying to do 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 in rails like this:

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

如果我从SQL Server运行查询直的SQL正确执行,但如果我运行ActiveRecord的版本,我只拿到冠军回来。

If I run the query straight from the sql server the sql executes correctly but if I run the activerecord version I only get the title back.

我想这可能是因为attr_accessible,但是这似乎没有已产生了效果。

I thought this might be because of attr_accessible but this doesnt seem to have made a difference.

什么想法?

推荐答案

您是否尝试过拨打方法收集对象?
此信息不包括在使用 to_s 法对象的输出,所以你可能只是看不到它,但总价值是存在的。

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

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

这篇关于Rails的3.1 - 发现使用count并选择以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆