mongoid/mongoid — Issue #1791: embeds_many - fun with clone
Author: banditj
Created: 2012-03-06 17:18:06+00:00
State: open

```ruby

# The models

class VProduct
include Mongoid::Document
include Mongoid::Timestamps
include Tire::Model::Search
include Tire::Model::Callbacks

field :name # name - Bezeichnung

belongs_to :v_group
embeds_many :p_images

#elastic search
index_name 'v-products'
def to_indexed_json
self.to_json
end
end

class SubCalculation
include Mongoid::Document
# Timestamps sind im Objekt Berechnung

############### RELATIONS #################
embedded_in :calculation
embeds_many :left_products, class_name: "VProduct"
embeds_many :right_products, class_name: "VProduct"
end

# The Problem
a = Calculation.last
# is a correct document with subcalculation.left_products
a.subcalculation.left_products << [VProduct.first.clone]
a.save

# now a new Calculation-Object is created with the attributes of clone VProduct
a = Calculation.last
-->Calculation-Document with all the cloned and wrong attributes of VProduct

#mongoid 2.4.6 , rails 3.1.3