我有一个消息类可以发送给用户。直到最近,它只能发送给一个用户,但不是要发送给多个用户。我决定在MessageProxy中包装消息(真的是一个装饰器,但这个词已经被Draper采用了)。为装饰器创建SimpleForm
class Message < ActiveRecord::Base
belongs_to :priority_type
end
class MessageProxy
def initialize(args = {})
@message = Message.new(args)
end
def method_missing(method_sym, *arguments, &block)
@message.send(method_sym, *arguments, &block)
end
end
我试图使用消息代理的形式,但我得到Association :priority_type not found
。这是特别奇怪,因为从我的rails控制台,MessageProxy行为与预期相同:
MessageProxy.new.priority_type # => nil instead of an exception
为什么我的代理转发的关联方法的消息对象?
我查看了SimpleForm源代码,发现问题是
in SimpleForm::FormBuilder.find_association_reflection(association)
。它看起来在@object.class
,但由于@object是我的装饰者,而不是代表,它无法找到关联。
如果我覆盖.class
使它像消息一样嘎嘎声,那么当我尝试呼叫MessageProxy.new.valid?
时,我也会启动消息验证程序。