Rails clone records

How can I and is it possible to clone records and related records?

Example.

Category
_ |
product ___
| Product option

So, when I click the button, cloning / copying categories, products and product options.

Thank...

+3
source share
2 answers

You must rewrite #clone so that all associations are also cloned, e.g.

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

... don't forget to do the same on Product and ProductVariant.

+4
source

, "". , , , . , , , , /. , , .

0

Source: https://habr.com/ru/post/1783192/


All Articles