我有一个模块:
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(用户)'为什么?
答案 0 :(得分:5)
您使用了include
,因此get_available_chair
是Store
的类方法;并且您无法从类方法中调用实例方法(get_chair_type
)。
如果您希望get_chair_type
成为一种类方法,请使用extend
代替include
。
答案 1 :(得分:0)
因为您已在aclass Store的范围内定义了get_available_chair。它是一种类方法。它无权访问实例方法get_chair_type。