我在访问范围时收到此错误。
这是 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
或它给出的任何其他范围时:
NoMethodError: undefined method `default_scoped?'
如果我将作用域配置为类方法,那么它就可以完美工作。
def self.registrations
where(code: 'registrations').first
end
请指导我理解并解决这个问题。
最佳答案
您所谓的范围
不是范围:它们不可链接。
我猜 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
关于ruby-on-rails - 访问范围时未定义方法 `default_scoped?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12365128/