我使用不能包含模块模型
Ruby version 1.8.7
Rails version 3.0.3
我有一个在我的Rails应用程序的每一个模型叫活着方法:
def alive
where('deleter is null')
end
我不想复制该代码每一个模型,所以我做了一个/lib/life_control.rb
module LifeControl
def alive
where('deleter is null')
end
def dead
where('deleter is not null')
end
end
,并在我的模型(例如client.rb)我说:
class Client < ActiveRecord::Base
include LifeControl
end
,在我的config/enviroment.rb我写这行:
require 'lib/life_control'
,但现在我得到一个无方法错误:
NoMethodError in
ClientsController#index
undefined method `alive' for
#<Class:0x10339e938>
app/controllers/clients_controller.rb:10:in
`index'
我究竟做错了什么?
你叫的类或实例级别的方法? – pythonandchips 2011-03-09 12:53:26
在课堂上的水平 – w1ng 2011-03-09 14:35:37