Rails如何为多态关联填充“model_type”字段?
我有一个活动模型。 它belongs_to :parent, :polymorphic => true
。
Rails是否使用parent.class.name
, parent.model_name
或其他东西来填充parent_type字段?
我希望Presenter的行为与它包装的父对象相似,我需要覆盖正确的方法。
谢谢。
我现在正在使用Rails 3.0.7,并且在active_record-3.0.7/lib/active_record/association.rb
,第1773行中定义了多态类型。
def create_belongs_to_reflection(association_id, options) options.assert_valid_keys(valid_keys_for_belongs_to_association) reflection = create_reflection(:belongs_to, association_id, options, self) if options[:polymorphic] reflection.options[:foreign_type] ||= reflection.class_name.underscore + "_type" end reflection end
所以看起来它正在调用class_name.underscore
然后附加“_type”。 对于rails 3.1,这可能略有不同,但这应该是一个很好的起点。