未定义的方法`default_scoped?在访问范围 [英] undefined method `default_scoped?' while accessing scope

查看:209
本文介绍了未定义的方法`default_scoped?在访问范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在同时访问范围此错误。

I am getting this error while accessing scopes.

下面是AR模型

class StatisticVariable < ActiveRecord::Base
  attr_accessible :code, :name

  has_many  :statistic_values

  scope :logins, where(code: 'logins').first
  scope :unique_logins, where(code: 'unique_logins').first
  scope :registrations, where(code: 'registrations').first

end

当我尝试用 StatisticVariable.logins 或任何其它范围内它提供了:

and when I try with StatisticVariable.logins or any other scopes it gives:

NoMethodError: undefined method `default_scoped?'

如果我配置范围为类的方法,然后它完美的作品。

If I configure scope as class method then it works perfectly.

def self.registrations
    where(code: 'registrations').first
end

请指引我了解并解决这个问题。

Please guide me to understand and fix this problem.

推荐答案

您所谓的范围不是范围:他们不是链式

Your so called scopes are not scopes: they aren't chainable.

我想Rails的尝试添加一个潜在的 default_scope 您的结果是导致失败。

I guess Rails tries to append a potential default_scope to your result which leads to failure.

做这样的事情:

  scope :logins, where(code: 'logins')
  scope :unique_logins, where(code: 'unique_logins')
  scope :registrations, where(code: 'registrations')

  def self.login
    logins.first
  end

这篇关于未定义的方法`default_scoped?在访问范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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