简体   繁体   English

Rails 的“平均”方法不起作用

[英]Rails' `average` method doesn't work

According to wikibooks.org and rubyonrails.org , this should be working:根据wikibooks.orgrubyonrails.org ,这应该有效:

Table.average(:score, :conditions => ['user_id=?', 1])

But it's not.但事实并非如此。 Why?为什么? When I'm doing above, my console says:当我在上面做时,我的控制台说:

SELECT AVG("table"."score") FROM "table"

No conditions at all.根本没有条件。

Eventually, I did what I want like this:最终,我做了我想做的事情:

Table.where('user_id=?', 1).average(:score)

But I'm still confused.但我还是很困惑。

You're trying to use so-called finders api which has been deprecated starting from Rails 4.0 and has been moved to a separate gem activerecord-deprecated-finders to ease migration from older versions.您正在尝试使用从 Rails 4.0 开始不推荐使用的所谓finders api ,并已移至单独的 gem activerecord-deprecated-finders以简化从旧版本的迁移。 Finders api has been completely removed starting from Rails 4.1.从 Rails 4.1 开始, Finders api已被完全删除

In light of the above your solution鉴于上述您的解决方案

User.where(user_id: 1).average(:score)

is correct.是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM