Reputation: 4065
How do i and is it possible clone records and it's associated records?
Example.
Category
_| Product
___| Product variant
So, when i click a button, the category, products and product variants is cloned/copied.
Thanks...
Upvotes: 3
Views: 2257
Reputation: 368
You must overwrite #clone so that all associations are also cloned, like
class Category < ActiveRecord::Base
has_many :products
alias_method :original_clone, clone
def clone
category = self.original_clone
category.products = self.products.clone
end
end
... remember to do the same on Product and ProductVariant.
Upvotes: 4
Reputation: 20724
I think you should start from using "clone" method. Your problem, if I have understood it right, is that you want a recursive clone. Frankly, i don't know if it exists a general solution but I think you could clone you father/sons chain by hand. Maybe it isn't an elegant solution but it should work.
Upvotes: 0