moiristo / deep_cloneable

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

Error deep cloning with named association #67

Closed gangleton closed 8 years ago

gangleton commented 8 years ago

I'm trying to deep clone an object with several associations:

Poll has_many :questions
Question has_many :options
Option  has_many :follow_up, :class_name => "Question", :foreign_key => "parent_option_id", :dependent => :destroy, :inverse_of => :option

When I try to deep clone a poll object, poll.deep_clone include: { questions: [ {options: :follow_up} ] } , I get the error NoMethodError: undefined method 'each' for #<Option:0x007fd87d419490>,

with the following trace:

activemodel-3.2.22.2/lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord-3.2.22.2/lib/active_record/attribute_methods.rb:149:in `method_missing'
activerecord-3.2.22.2/lib/active_record/associations/collection_association.rb:310:in `replace'
activerecord-3.2.22.2/lib/active_record/associations/collection_association.rb:41:in `writer'
activerecord-3.2.22.2/lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
deep_cloneable-2.2.1/lib/deep_cloneable.rb:127:in `block in dup_has_many_association'
activerecord-3.2.22.2/lib/active_record/associations/collection_proxy.rb:91:in `collect'
activerecord-3.2.22.2/lib/active_record/associations/collection_proxy.rb:91:in `method_missing'
deep_cloneable-2.2.1/lib/deep_cloneable.rb:124:in `dup_has_many_association'

It seems to be an issue when the association doesn't use the same name as the class of the object, similar to https://github.com/moiristo/deep_cloneable/issues/58

Any idea how to get around this?

moiristo commented 8 years ago

You're follow_up association looks a bit mangled, what's going on there?

What actually happens (I think) is the following:

gangleton commented 8 years ago

follow_up is a naming problem that I inherited with this code base, I know it's not correct.

A follow_up is a question that belongs to an option. Options have two associations with Questions- one as a belongs_to, and one as has_many. The other side of that is

Question belongs_to :option, :foreign_key => "parent_option_id", inverse_of: :follow_up

The foreign key is stored on Question.

moiristo commented 8 years ago

Ok, I need to reproduce the error in the test suite first in order to fix it. I'll try to add one shortly.. unless you'd like to give it a try yourself of course ;)

gangleton commented 8 years ago

I should be able to do that this afternoon…

On Sep 13, 2016, at 12:12 PM, Reinier de Lange notifications@github.com wrote:

Ok, I need to reproduce the error in the test suite first in order to fix it. I'll try to add one shortly.. unless you'd like to give it a try yourself of course ;)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/moiristo/deep_cloneable/issues/67#issuecomment-246791604, or mute the thread https://github.com/notifications/unsubscribe-auth/AAM3e8imrYqFU82P5OAtKj4mMI4oeEXbks5qpvWdgaJpZM4J7DFl.

moiristo commented 8 years ago

This should be fixed by #71 (release 2.2.2).