moiristo / deep_cloneable

This gem gives every ActiveRecord::Base object the possibility to do a deep clone that includes user specified associations.
MIT License
785 stars 89 forks source link

cannot deep clone my projects #58

Closed lironbeni closed 8 years ago

lironbeni commented 8 years ago

Hi, I'm trying to deep clone my project, it has elements, and folders with experiments and elements also, all has belongs_to and has_many relationships. my code is this:

source_project = Projects::Project.find_by_id(params[:id])
cloned_project = source_project.deep_clone(include: [:elements, { folders: [:experiments, :elements] }])

the error I'm getting is this:

undefined method `each' for #Projects::Project:0x007fc9547a6730

and the full (relavent)trace is this:

NoMethodError - undefined method `each' for #<Projects::Project:0x007fc9547a6730>:
  activemodel (4.2.5) lib/active_model/attribute_methods.rb:433:in `method_missing'
  activerecord (4.2.5) lib/active_record/associations/collection_association.rb:365:in `replace'
  activerecord (4.2.5) lib/active_record/associations/collection_association.rb:47:in `writer'
  activerecord (4.2.5) lib/active_record/associations/builder/association.rb:123:in `samples='
  deep_cloneable (2.2.0) lib/deep_cloneable.rb:123:in `block in dup_has_many_association'
  activerecord (4.2.5) lib/active_record/relation/delegation.rb:46:in `collect'
  deep_cloneable (2.2.0) lib/deep_cloneable.rb:120:in `dup_has_many_association'
  deep_cloneable (2.2.0) lib/deep_cloneable.rb:76:in `block in deep_clone'
  deep_cloneable (2.2.0) lib/deep_cloneable.rb:40:in `deep_clone'
  app/controllers/knowledge/projects_controller.rb:363:in `duplicate'

What am I missing? Or doing wrong? Please help....

moiristo commented 8 years ago

It's a bit strange. It's duping a has_many association, but it fails when it tries to set its reverse association. The reverse association found is called samples, but it passes an instance of Project to it. The reverse association should have been something like belongs_to :project, but it found samples instead.

I need to know what those associations look like in your code before I can tell you more.

moiristo commented 8 years ago

Closing this because of lack of information & repro.