5

我有一个活动模型。它belongs_to :parent, :polymorphic => true

Rails 是否使用parent.class.nameparent.model_name其他东西来填充 parent_type 字段?

我希望 Presenter 表现得像它包装的父对象,我需要重写正确的方法。

谢谢。

4

1 回答 1

6

我现在正在使用 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 可能略有不同,但这应该是一个很好的起点。

于 2011-12-02T00:00:24.707 回答