Skip to content
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

Closed
atstockland opened this issue May 7, 2014 · 4 comments
Closed

Doesn't clone all children #34

atstockland opened this issue May 7, 2014 · 4 comments

Comments

@atstockland
Copy link

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
Copy link
Owner

moiristo commented May 7, 2014

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

moiristo commented May 7, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants