Ruby on rails 3 ActiveRecord关联上的装饰器模式和类型不匹配
我正在使用decorator类向我的模型添加一些功能。我只希望这些信息用于处理某些值,而不是将任何内容保存到数据库中 我有我的装饰课,比如:Ruby on rails 3 ActiveRecord关联上的装饰器模式和类型不匹配,ruby-on-rails-3,activerecord,decorator,Ruby On Rails 3,Activerecord,Decorator,我正在使用decorator类向我的模型添加一些功能。我只希望这些信息用于处理某些值,而不是将任何内容保存到数据库中 我有我的装饰课,比如: class Deco def initialize o @target = o end def method_missing method, *args, &block @target.send(method, *args, &block) end #my extra methods en
class Deco
def initialize o
@target = o
end
def method_missing method, *args, &block
@target.send(method, *args, &block)
end
#my extra methods
end
我是这样使用它的:
deco_model = Deco.new(model)
这里的问题是,当我想将此deco_模式
与另一个模式关联时,我会得到一个类型不匹配错误,这是有意义的,但如果我将以下方法添加到我的decorator类中:
def class
@target.class
end
我仍然得到同样的错误,但它说:模型(#aaaaaa)预期,得到模型(#aaaaaa)
是的,“模型”将是我的模型的类,并且在这两种情况下对象id是相同的。。。那么,如果对象id相同,为什么我仍然得到异常
谢谢原来我还必须重写
是ActiveRecord::associations::Association
中使用的方法
完成后:
def is_a? klass
@target.class.object_id == klass.object_id
end
异常不再被抛出。使用SimpleDelegator可能更容易,比如:class Deco