Question

Given the following code:

user_decorator.rb

Refinery::User.class_eval do
  has_many :employees, :class_name => 'Refinery::Employee'

  scope :active_employees,
        lambda {
          joins(:employees).merge(::Refinery::Employee.active)
        }

end

employee.rb

module Refinery
  class Employee < ActiveRecord::Base
    belongs_to :user, :class_name => 'Refinery::User'

    scope :active, where(:retired => false)
  end
end

running rails console...

> ::Refinery::User.active_employees
 => [ .......... lots of records etc ]
> user = ::Refinery::User.where(:username => 'test').first
 => #<Refinery::User .... etc etc>
> user.active_employees
NoMethodError: undefined method `active_employees' for #<Refinery::User:0x000000051330f8>

What am I doing wrong?

Was it helpful?

Solution

A scope is related to the model, and not to an instance of that model.

If you want to do that, you have to create a method that does the same.

At least, I think so...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top