moiristo / deep_cloneable

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

Doesn't clone all children #34

Closed atstockland closed 10 years ago

atstockland commented 10 years ago

Sorry for no specs. I'm hoping this is just a misunderstanding on my end.

The following will clone 2 of the total 28 child flight lessons and 0 scenarios. Course.find(2).dup(include: {:flightlessons => :scenarios}).update_attribute(:farpart_id, Farpart.find(1).id)

updating the farpart_id is needed to pass a scoped unique validation

Course has_many :flightlessons has_many :scenarios, through: :flightlessons (I get more cloned flight lessons if I comment this out)

Flightlesson has_many :scenarios, as: :lesson, dependent: :destroy # poly

How can I alter this to get an accurate clone of child flightlessons: :scenarios?

moiristo commented 10 years ago

I'm not sure yet.. when you add a debugger after calling dup (before update_attribute), what does the object look like? Does it contain dups of all included data? The thing is that update_attribute might not do what you want. You should be calling save or update_attributes instead, as those methods will run callbacks.

atstockland commented 10 years ago

You're right. The callback on either of those methods fails due to my own validations. Where as update_attribute was not.

I need to update that one attribute to pass a scoped uniqueness validation. How (when) would I update it?

Thanks for the gem!

Adam

On May 7, 2014, at 12:41 AM, Reinier de Lange notifications@github.com wrote:

I'm not sure yet.. when you add a debugger after calling dup (before update_attribute), what does the object look like? Does it contain dups of all included data? The thing is that update_attribute might not do what you want. You should be calling save or update_attributes instead, as those methods will run callbacks.

— Reply to this email directly or view it on GitHub.

atstockland commented 10 years ago

I apologize if this is an annoying question you get. Would you be willing to get online and take a peak at my implementation of this gem? I'd happily pay. This gem is a great way to dup...but, my model is somehow preventing things from working. adamstockland at gmail.

Adam

On May 7, 2014, at 12:41 AM, Reinier de Lange notifications@github.com wrote:

I'm not sure yet.. when you add a debugger after calling dup (before update_attribute), what does the object look like? Does it contain dups of all included data? The thing is that update_attribute might not do what you want. You should be calling save or update_attributes instead, as those methods will run callbacks.

— Reply to this email directly or view it on GitHub.

moiristo commented 10 years ago

First of all, I'd change the code to something like this:

course_copy = Course.find(2).dup(include: {:flightlessons => :scenarios})
course_copy.farpart = Farpart.find(1)
puts course_copy.errors.full_messages.to_sentence unless course_copy.valid? # some debugging
course_copy.save!

If it won't save while the copy is valid, then it might be the case that you've defined before_create or before_save callbacks in your models that return 'false', causing the save to fail.