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

How To Attach Polymorphic Associated Records To A Different Model? #124

Closed
Petercopter opened this issue Aug 26, 2020 · 2 comments
Closed

Comments

@Petercopter
Copy link

Let's say I have a Parrot model, and it's got a polymorphic association with Pirate and Landlubber.

I want to deep clone a Pirate's Parrots and associate them with the Landlubber. I did something similar for a non-polymorphic model, and that worked great (Basically this #109)

So I do

@pirate.deep_clone(include: :parrots, except: [{ parrots: [:parrotable_type] }])

I'm ending up with

[#<Parrot:0x0000559e5add0498 id: nil, name: 'Crackers', parrotable_type: "Pirate", parrotable_id: nil, created_at: nil, updated_at: nil>]

parrotable_type is not cleared, but parrotable_id is cleared.

Is this working as intended?

@moiristo
Copy link
Owner

moiristo commented Aug 26, 2020

When you deep clone an association, it sets the association (pirate parrots) as well as the inverse association (parrot parrotable). I think rails sets the parrotable_type when the duped record is associated. There is no id as the pirate is a new record.

Does it really matter in this case? As you want to associate LandLubber afterwards, I guess you could do the same thing (set the inverse)? Something like:

land_lubber = LandLubber.new
land_lubber.parrots = duped_pirate.parrots.tap { |parrots| parrots.each { |parrot| parrot.parrotable = land_lubber } }

@Petercopter
Copy link
Author

@moiristo Thanks, that does make sense. Love that tap, that's some true Rubyist style there 👍

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