我有代表的一些方法和屬性的不同模型的模型,假設Rails的委託的屬性沒有@屬性,read_attribute返回nil
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
的問題是,我可以通過調用方法訪問'some_property',但不能通過read_attribute訪問。
> 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獲取屬性值?