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::Cha开发者_如何学Pythonir
, but I got the error undefined method 'get_chair_type(user)' why?
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
.
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.
精彩评论