moiristo / deep_cloneable

This gem gives every ActiveRecord::Base object the possibility to do a deep clone that includes user specified associations.
MIT License
785 stars 89 forks source link

include:do loop also modified the includes #123

Closed aximuseng closed 4 years ago

aximuseng commented 4 years ago

Here is my deep clone code:

@duplicate = @budget.deep_clone include: :budget_items, except: [:slug, :comments_count, :uploads_count ] do |original, duplicate|
  duplicate.name = "#{original.name} Copy"
end

The expected action is that the budget.name has Copy appended. The issue is that my budget_items model also has a name attribute and when deep cloned those associated records get modified also.

moiristo commented 4 years ago

Hello,

This is documented behavior, please see the note in the Optional Block section. You should check the type of the object in the block: duplicate.name = "#{original.name} Copy" if original.is_a?(Budget)