-
Notifications
You must be signed in to change notification settings - Fork 89
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
Doesn't clone all children #34
Comments
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
|
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
|
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?
The text was updated successfully, but these errors were encountered: