Reputation: 1726
I'm having issues with the referencing of a specific model when being cloned. I've tried using multiple approaches but none have gotten the correct result, and I believe it has to do with me not understanding how the logic of cloning works.
This involves multiple models, but the error raises in an interdependency between the last 2 models in the hierarchy. For simplicity, I'll try to explain this with 3 models to get an idea of how everything's working: Page, PageElement and PageElementDependency
Page
has many PageElements
PageElement
has many dependent_page_element_dependencies
and has_many dependee_page_element_dependencies
, where both of these relations are PageElementDependency
. Through both of these dependencies I've setup the following has_many through relationships: has_many :dependees, through: :dependent_page_element_dependencies
and has_many :dependents, through: :dependee_page_element_dependencies
. Summarizing everything looks like this:#models/page.rb
has_many :page_elements, -> { order(position: :asc) }, inverse_of: :page, dependent: :destroy
amoeba do
enable
clone [:page_elements]
end
#models/page_element.rb
has_many :dependent_page_element_dependencies, foreign_key: :dependent_id, inverse_of: :dependent, class_name: "PageElementDependency", dependent: :destroy
has_many :dependees, through: :dependent_page_element_dependencies
has_many :dependee_page_element_dependencies, foreign_key: :dependee_id, inverse_of: :dependee, class_name: "PageElementDependency", dependent: :destroy
has_many :dependents, through: :dependee_page_element_dependencies
amoeba do
enable
clone [:dependee_page_element_dependencies, :dependent_page_element_dependencies]
end
#models/page_element_dependency.rb
belongs_to :dependent, class_name: "PageElement", inverse_of: :dependent_page_element_dependencies, foreign_key: :dependent_id
belongs_to :dependee, class_name: "PageElement", inverse_of: :dependee_page_element_dependencies, foreign_key: :dependee_id
Let's say I'm trying to clone Page#3, with PageElement#4 and PageElement#5, where there's a PageElementDependency, where the dependent is PageElement#4 and the dependee (who is depended upon) is PageElement#5.
So basically here we'd be able to access PageElement.find(5).dependees
=> PageElement#6
, and if I do PageElement.find(6).dependents
=> PageElement#5
.
So, when trying to clone Page#3, I'd expected to get a new Page#4 with PageElement#6 and PageElement#7, and here's the important part, a PageElementDependency where the dependent is PageElement#6 and the dependee is PageElement#7.
However, when I try cloning the PageElementDependencies, only one of the columns is getting a new value, but the other column is keeping the old PageElement's value. So in the example given, I'd get a PageElementDependency where the dependee is correct: PageElement#6, but the dependent is not update and stays as PageElement#5.
I'm not sure how to use a remapper in order to get the related "new" and "old" object of the dependencies, so I'm not really sure how to proceed.
I've also posted this as an issue in github: https://github.com/amoeba-rb/amoeba/issues/84
Upvotes: 3
Views: 401