Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge PR 170 for Rails 5 Upgrade #2

Merged
merged 10 commits into from
Aug 16, 2017
Prev Previous commit
Next Next commit
Fixes restoring polymorphic has_one relationships
If association 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).

If it is present, that will be the type column on the polymorphic model.
The foreign key can be found as an attribute on association.
  • Loading branch information
PatKoperwas committed Dec 4, 2014
commit 99999dddcbcceb8438a9f4064f1849b04a2824e0
12 changes: 10 additions & 2 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,16 @@ def restore_associated_records(deleted_at, window)

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
where_conditions = { association_polymorphic_type => self.class.name.to_s, association_foreign_key => self.id }
else
where_conditions = { association_foreign_key => self.id }
end

Object.const_get(association_class_name).only_deleted.where(where_conditions).first.try(:restore, recursive: true)
end
end

Expand Down