开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜