moiristo/deep_cloneable

Cant understand how to work with deep_cloneable

sonq opened this issue · 1 comments

sonq commented

Hey guys,

i am relatively new to rails and to be honest did not understand documentation so far. I have a projects to learn rails and i want to use deep_cloneable functionality.

Basically i have products and productlines. Products has many productlines and productlines belongs to product. I want to duplicate product and its lines when i click on a button but here is my problem: i did not understand how to created methond? should i create it in controller level or in model? How to call this function from my view? Also, do i need to add anything in my routes?

I would be really glad if someone can show how to use this gem (for dummies :))

Hi,

In your case, I'd typically use it in a controller. The action would look something like this:

class ProductsController < ApplicationController
  before_action do
    @product = Product.find(params[:id)
  end

  def duplicate
    duplicated_product = @product.deep_clone(include: :product_lines)
    duplicated_product.save!

    redirect_to product_url(duplicated_product)  
  end
end

How to link this action to a button in a view is really out of scope here. You should start with a simple introduction to Rails first. Something like this: https://guides.rubyonrails.org/getting_started.html