Closed atstockland closed 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.
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.
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.
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.
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?