Mellon
Mellon

Reputation: 38842

Undefined method, why?

I have a module:

module Room::Chair

  def get_chair_type(user)
    ..
  end

end

Then, I have a class with a class method 'self.get_available_chair' which invoke the 'get_chair_type' method in Room::Chair module:

class Store < ActiveRecord::Base
  include Room::Chair

   def self.get_available_chair(user)
       my_chair=get_chair_type(user) # error: undefined method 'get_chair_type'
   end

end

I have include Room::Chair, but I got the error undefined method 'get_chair_type(user)' why?

Upvotes: 1

Views: 507

Answers (2)

RameshVel
RameshVel

Reputation: 65877

Because you have defined get_available_chair in the scope of aclass Store. Its a class method. It doesn't have the access to the instance method get_chair_type.

Upvotes: 0

tokland
tokland

Reputation: 67860

You used include, so get_available_chair is a classmethod of Store; and you cannot call an instance method (get_chair_type) from a classmethod.

If you want get_chair_type to be a classmethod, use extend instead of include.

Upvotes: 5

Related Questions