未定义的方法,为什么?

Mel*_*lon 1 ruby ruby-on-rails ruby-on-rails-3

我有一个模块:

module Room::Chair

  def get_chair_type(user)
    ..
  end

end
Run Code Online (Sandbox Code Playgroud)

然后,我有一个类方法' self.get_available_chair ',它在模块中调用' get_chair_type '方法Room::Chair:

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
Run Code Online (Sandbox Code Playgroud)

我有include Room::Chair,但我得到错误undefined方法'get_chair_type(用户)'为什么?

tok*_*and 5

你使用过include,所以get_available_chair是一种类别的方法Store; 并且你不能get_chair_type从classmethod 调用实例方法().

如果你想get_chair_type成为一个类方法,请使用extend而不是include.