Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `default_scoped?' while accessing scope

I am getting this error while accessing scopes.

Here is AR model

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

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.

like image 926
Amit Patel Avatar asked Sep 11 '12 07:09

Amit Patel


1 Answers

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

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

Do something like:

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

  def self.login
    logins.first
  end
like image 72
apneadiving Avatar answered Nov 03 '22 01:11

apneadiving