ruby-on-rails – Rails如何在“model_type”字段中填充多态关联
发布时间:2020-12-17 03:21:08 所属栏目:百科 来源:网络整理
导读:我有一个活动模型.它belongs_to:parent,:polymorphic =真正. Rails是否使用parent.class.name,parent.model_name或其他东西来填充parent_type字段? 我希望Presenter的行为与它包装的父对象相似,我需要覆盖正确的方法. 谢谢. 解决方法 我现在正在使用Rails
我有一个活动模型.它belongs_to:parent,:polymorphic =>真正.
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,这可能略有不同,但这应该是一个很好的起点. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |