我有一个模型,它将一些方法和属性委托给不同的模型,比如说
class ModelOne < ActiveRecord::Base
# this model has some_property column in database
end
和
class ModelTwo < ActiveRecord::Base
belongs_to :model_one
delegate :some_property, :to => :model_one
end
问题是我可以通过调用该方法而不是通过 read_attribute 来访问“some_property”。
> obj1 = ModelTwo.last
> obj1.some_property
=> "some value"
> obj1.read_attribute :some_property
=> nil
> obj1.inspect
=> "#ModelTwo ... , ... , some_property: nil "
可以设置此属性:
> obj1.some_property = "some value"
> obj1.inspect
=> "#ModelTwo ... , ... , some_property: "some value" "
所以我可以通过调用它而不是通过 read_attribute 或通过检查来访问委托属性。有没有机会通过read_attribute获取属性值?