Commit 723ec506 by Patrick Koperwas Committed by John Hawthorn

Fixes restoring polymorphic has_one relationships

If the association object is a has_one relationship with an :as option,
it will have a type attribute (see
https://github.com/rails/docrails/blob/master/activerecord/lib/active_record/reflection.rb#L280).

If this type attributes is present, that will be the type column on the
polymorphic model. The foreign key can be found as an attribute on
the association object.
parent ed7793d4
......@@ -143,8 +143,16 @@ module Paranoia
if association_data.nil? && association.macro.to_s == "has_one"
association_class_name = association.options[:class_name].present? ? association.options[:class_name] : association.name.to_s.camelize
association_foreign_key = association.options[:foreign_key].present? ? association.options[:foreign_key] : "#{self.class.name.to_s.underscore}_id"
Object.const_get(association_class_name).only_deleted.where(association_foreign_key => self.id).first.try(:restore, recursive: true)
association_foreign_key = association.foreign_key.present? ? association.foreign_key : "#{self.class.name.to_s.underscore}_id"
if association.type
association_polymorphic_type = association.type
association_find_conditions = { association_polymorphic_type => self.class.name.to_s, association_foreign_key => self.id }
else
association_find_conditions = { association_foreign_key => self.id }
end
Object.const_get(association_class_name).only_deleted.where(association_find_conditions).first.try(:restore, recursive: true)
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment