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

Fails to duplicate descendants of polymorphic relations #31

Open
Benoit-Baumann opened this issue Jan 8, 2024 · 1 comment
Open

Fails to duplicate descendants of polymorphic relations #31

Benoit-Baumann opened this issue Jan 8, 2024 · 1 comment

Comments

@Benoit-Baumann
Copy link

Hi @hopsoft !

First of all, thanks for this awesome gem !

I'm facing a problem while trying to duplicate a record with complexe associations. I have the following models (simplified):

class ContentPage < ApplicationRecord
  has_many :content_page_blocks, inverse_of: :content_page, dependent: :destroy
end

class ContentPageBlock < ApplicationRecord
  belongs_to :content_page
  belongs_to :blockable, polymorphic: true, dependent: :destroy, inverse_of: :content_page_block
end

class CardBlock < ApplicationRecord
  has_one :content_page_block, as: :blockable, dependent: :destroy, inverse_of: :blockable
  accepts_nested_attributes_for :content_page_block, allow_destroy: true

  has_one :content_page, through: :content_page_block

  has_many :cards class_name: "ContentPageCard", inverse_of: :card_block, dependent: :destroy
end

# more ApplicationRecord models for other blocks...

My goal is to duplicate a content page with all its descendants.

options = {
  include_keys: false,
  include_timestamps: false,
  include_unsaved_changes: false,
  include_descendants: true,
  descendant_depth: 10
}

content_page = ContentPage.first
=> #<ContentPage:0x0000000108fdae68
   id: 1,
   name: "First ContentPage">

# load descendants into memory
content_page.content_page_blocks.map(&:blockable)

content_page.content_page_blocks.first
=> #<ContentPageBlock:0x00000001096c8a90
   id: 1,
   content_page_id: 1,
   blockable_type: "CardBlock",
   blockable_id: 1>

content_page.content_page_blocks.first.blockable
=> #<CardBlock:0x000000010978b860
   id: 1,
   title: "First CardBlock">

encoded = URI::UID.build(content_page, options).to_s
content_page_dup = URI::UID.parse(encoded).decode

content_page_dup
=> #<ContentPage:0x00000001089c8e58
   id: nil,
   name: "First ContentPage">
# the content page is correctly duplicated 🥳

content_page_dup.content_page_blocks.first
=> #<ContentPageBlock:0x0000000108a30918
   id: nil,
   content_page_id: nil,
   blockable_type: "CardBlock",
   blockable_id: nil>
# the content page block is correctly duplicated as well 🥳

content_page_dup.content_page_blocks.first.content_page == content_page_dup
=> true # and it's correctly associated to the duplicated content page 🥳

content_page_dup.content_page_blocks.first.blockable
=> nil # it should be a new instance of CardBlock but it is nil... 😕

As you can see, the gem fails to duplicate the polymorphic association blockable of the ContentPageBlock model.

FYI, I'm encoutering the same problem with models that have has_many_attached and has_one_attached associations.

Thanks for your help !

@hopsoft
Copy link
Owner

hopsoft commented Jan 10, 2024

Thanks for the report. I definitely want to expand on the current "simple" descendant support and tackle other types of associations soon.

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