Gpcnec76
Gpcnec76

Reputation: 69

amoeba not cloning all :has_many associations

I am using the Amoeba gem to clone a model and all children. The gem is working well with one exception - there is one :has_many association that is not being picked up.

My parent Model is Option:

class Option < ActiveRecord::Base

has_many :products, as: :productable, dependent: :destroy
has_many :censusinfos, :autosave => true

belongs_to :rating

accepts_nested_attributes_for :censusinfos


amoeba do
  enable
end

# other code.....

Products is being cloned appropriately, but the issue is on :censusinfos. That model is defined as:

class Censusinfo < ActiveRecord::Base

has_many :census_sheets
has_many :census_fields
belongs_to :option

#other code......

CensusField children are copied correctly, but CensusSheet is not being cloned.

Any thoughts/ideas why??

Thanks!

Greg

Upvotes: 0

Views: 983

Answers (2)

fabOnReact
fabOnReact

Reputation: 5942

I read the documentation at the following link

ActiveRecord: How can I clone nested associations?

Should't you enable recursive copying of associations by including in class Censusinfo amoeba do enable end?

class Censusinfo < ActiveRecord::Base

has_many :census_sheets
has_many :census_fields
belongs_to :option

amoeba do
  enable
end

Thanks

Fabrizio

Upvotes: 1

Gpcnec76
Gpcnec76

Reputation: 69

I needed to add "enable" to Censusinfo. Example below. Thanks to Fabrizio!

class Censusinfo < ActiveRecord::Base

has_many :census_sheets
has_many :census_fields
belongs_to :option

amoeba do
  enable
end

#other code...... 

Upvotes: 0

Related Questions