提交 dce398d5 编写于 作者: C Carlos Antonio da Silva

Avoid an attempt to fetch old record when id was not present in touch callback

上级 034f254d
......@@ -66,19 +66,19 @@ def belongs_to_counter_cache_after_update_for_#{name}
def add_touch_callbacks(reflection)
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def belongs_to_touch_after_save_or_destroy_for_#{name}
record = #{name}
foreign_key_field = #{reflection.foreign_key.inspect}
old_foreign_id = attribute_was(foreign_key_field)
if old_foreign_id
reflection_klass = #{reflection.klass}
old_record = reflection_klass.find_by(reflection_klass.primary_key => old_foreign_id)
foreign_key_field = #{reflection.foreign_key.inspect}
if changed_attributes.key?(foreign_key_field)
reflection_klass = #{reflection.klass}
primary_key_field = reflection_klass.primary_key
old_foreign_id = changed_attributes[foreign_key_field]
old_record = reflection_klass.where(primary_key_field => old_foreign_id).first
if old_record
old_record.touch #{options[:touch].inspect if options[:touch] != true}
end
end
record = #{name}
unless record.nil? || record.new_record?
record.touch #{options[:touch].inspect if options[:touch] != true}
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册