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

Not creating new records while cloning association has_many: through: #86

Closed NarasimhaReddyY closed 5 years ago

NarasimhaReddyY commented 6 years ago

Hello,

I observed one thing while cloning nested associations. Whenever am trying to clone a has_many through association,deep_clone instead creating new association objects, it just assigning association records of original record to cloned record.

ex:- if Model has association has_many: :somethings, through: :anothers here let's say Something.count == > 10

if i do clone = Model.find(1).deep_clone include: [:somethings] clone.save! after cloning also Something.count == 10

Is it a intented functionality or bug? Thanks

FYI:

ruby - 2.3.3p222 Rails - 4.2.4 deep_clonable - 2.2.2

moiristo commented 6 years ago

Hi,

I think it is, this is related to #22. I think you should clone via the through association instead:

Model.find(1).deep_clone include: { another: :something }
moiristo commented 6 years ago

27 is also related.

NarasimhaReddyY commented 6 years ago

@moiristo Tried both before raising a issue here. Those solutions seems to not working with the versions i provided. Please take a look into it. Thanks.

When i try Model.find(1).deep_clone include: { anothers: :somethings } it's raising a exception ActiveRecord::Base::DeepCloneable::AssociationNotFoundException: Another#somethings

moiristo commented 5 years ago

I'm closing this. Based on your last message, I think this was not a deep_cloneable issue and was probably caused by incorrectly defined associations.