未定义的方法`default_scoped?' 在访问范围时

Ami*_*tel 17 activerecord named-scope ruby-on-rails ruby-on-rails-3

我在访问范围时遇到此错误.

这是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
Run Code Online (Sandbox Code Playgroud)

当我尝试使用StatisticVariable.logins或任何其他范围时,它给出:

NoMethodError: undefined method `default_scoped?'
Run Code Online (Sandbox Code Playgroud)

如果我将范围配置为类方法,那么它可以完美地工作.

def self.registrations
    where(code: 'registrations').first
end
Run Code Online (Sandbox Code Playgroud)

请指导我理解并解决这个问题.

apn*_*ing 29

你所谓scopes的不是范围:它们不是可链接的.

我猜Rails试图为default_scope你的结果增加一个导致失败的结果.

做类似的事情:

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

  def self.login
    logins.first
  end
Run Code Online (Sandbox Code Playgroud)