未定义的方法,为什么?

时间:2011-03-22 13:15:16

标签: ruby-on-rails ruby ruby-on-rails-3

我有一个模块:

module Room::Chair

  def get_chair_type(user)
    ..
  end

end

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

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

我有include Room::Chair,但我收到错误未定义的方法'get_chair_type(用户)'为什么?

2 个答案:

答案 0 :(得分:5)

您使用了include,因此get_available_chairStore的类方法;并且您无法从类方法中调用实例方法(get_chair_type)。

如果您希望get_chair_type成为一种类方法,请使用extend代替include

答案 1 :(得分:0)

因为您已在aclass Store的范围内定义了get_available_chair。它是一种类方法。它无权访问实例方法get_chair_type。