Skip to main content

How do I create a duplicate with associations included?

Rails • Asked by shesso
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.

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

Also, do you have any validations on your Project or Task model not included in the snippets? 

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.

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 =\

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):

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!

Excellent, glad you were able to get it figured out!

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.