Closed Petercopter closed 4 years ago
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 } }
@moiristo Thanks, that does make sense. Love that tap
, that's some true Rubyist style there 👍
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 https://github.com/moiristo/deep_cloneable/issues/109)
So I do
I'm ending up with
parrotable_type
is not cleared, butparrotable_id
is cleared.Is this working as intended?