Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I create a duplicate with associations included?

shesso asked in Rails
I'm fairly new to Rails and still learning. I recently stumbled upon a problem that I'm sure there is an easy way to fix it, but I haven't figured it out yet.

In the application I'm trying to create there are projects. Each project has multiple tasks. Once one project is set up with all tasks I want to use this as a template for creating new projects (with the same set of tasks). Some of the code contains snippets from episodes here on GoRails (the tasks are sortable etc). Cocoon is used for nested forms.

I have been trying to create duplicates of the template with and without deep_clonable and amoeba, but they are not picking up the associated tasks in the forms.
I'm pretty much at a stand still atm. 

class Project < ApplicationRecord

  belongs_to :user

  has_many :tasks, inverse_of: :project, dependent: :destroy
  accepts_nested_attributes_for :tasks, reject_if: :all_blank, allow_destroy: true

  amoeba do
    enable
    prepend :title => "Copy of "
  end

end
(The amoeba part is not present when not in use in the controller.)
class Task < ApplicationRecord

  belongs_to :project, touch: true

end

Try 1
from  projects_conroller.rb - An online answer which seem to work for some.
  def clone
    @project = Project.find(params[:id])
    @project = Project.new(@project.attributes)
    render 'new'
  end
Tasks show up, but throws an error on save.
Completed 401 Unauthorized in 19ms (ActiveRecord: 3.7ms)
ActiveRecord::RecordNotFound (Couldn't find Task with ID=1 for Project with ID=):  
app/controllers/projects_controller.rb:32:in `create'

Try 2 - deep_clonable
  def clone
    @project = Project.find(params[:id]).deep_clone include: :tasks, except: [ { task: [:status ] } ], only: :title    
    render 'new'
  end
Tasks are not showing up in form. Even when used in its simplest form with only include:

Try 3 - amoeba
 def clone
    @project = Project.find(params[:id]).amoeba_dup    
    render 'new'
  end
Tasks are not showing up in form. 

I'm guessing this may be solved with a single line of code by more experienced programmers. 
Any help is greatly appreciated.
Reply
Hey shesso, 

At first glance, it looks like you need to add the association to your amoeba block:

amoeba do
  enable
  include_association :tasks
  prepend :title => "Copy of "
end

With this done, re-try your 3rd attempt
Reply
Also, do you have any validations on your Project or Task model not included in the snippets? 
Reply
Sorry. I've been adding and removing parts in the amoeba code to see if anything changes.
Added the include association back in, but the tasks still do not show up.
Reply
Oh,  and on your clone method, you need to call save:

def clone
  @project = Project.find(params[:id]).amoeba_dup.save
  render 'new'
end

That's probably your real issue - I just pulled up one of my projects where I used amoeba and noticed I had to call save on it. Sorry, I should have done that first =\
Reply

Adding .save creates the project and connects copies of the tasks!

but I get a new error stopping the form view being loaded
ActionView::Template::Error (undefined method `model_name' for true:TrueClass):

Reply
Error seems to be fixed by changing the code to
  def clone
    @project = Project.find(params[:id]).amoeba_dup
    @project.save
    render 'new'
  end

Thanks for your help, Jacob!
Reply
Excellent, glad you were able to get it figured out!
Reply

hi, it's really helpful. i also have a case like this, its a quotation and invoice.

does the steps, also copy the association or just the id and refer it to the tasks from previous project?

thanks.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.