我有一个事件模型。它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 来说可能略有不同,但这应该是一个很好的起点。
关于ruby-on-rails - Rails 如何为多态关联填充 "model_type"字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8350037/