目前我在我的事件模型中
class Event < ActiveRecord::Base
def self.cattr_accessor_with_default(name, value = nil)
cattr_accessor name
self.send("#{name}=", value) if value
end
cattr_accessor_with_default :fixed_start_end_times, false
...
正如我定义的调度程序模块
class Event < ActiveRecord::Base
include Scheduler
看来我无法将 cattr_accessor_with_default 方法移动到此调度程序模块中
module Scheduler
def self.included(base)
base.send :include, InstanceMethods
base.extend ClassMethods
end
module ClassMethods
def cattr_accessor_with_default(name, value = nil)
cattr_accessor name
self.send("#{name}=", value) if value
end
cattr_accessor_with_default :fixed_start_end_times, false
...
引发错误:Scheduler::ClassMethods:Module (NoMethodError) 的未定义方法“cattr_accessor_with_default”
我也尝试使用更好的 Rails3 方式
module Scheduler
def self.included(base)
base.send :include, InstanceMethods
base.extend ClassMethods
end
module ClassMethods
class_attribute :fixed_start_end_times
self.fixed_start_end_times = false
Scheduler::ClassMethods:Module 的相同错误未定义方法“class_attribute”