deep_clone gem不复制created_at

时间:2016-01-08 15:41:31

标签: ruby ruby-on-rails-4 dup

我正在复制一个rails对象,它除了对象的created_at值以外的所有细节都很复制。我正在使用deep_clone gem进行深度复制。

这是代码。我想要raw_materials和costing_items的created_at值。

@costing = @old_costing.deep_clone :include => [{style: :images}, {raw_materials: :costing_items} , :other_cost_fixeds, :other_costs, :exchange_rates ], :use_dictionary => true do |original, kopy|
                kopy.remote_picture_url = original.picture_url if kopy.is_a?(Image)
            end

1 个答案:

答案 0 :(得分:0)

使用deep_clone似乎很简单,可以在do块中完成,如果我们这样做

@costing = @old_costing.deep_clone :include => [{style: :images}, {raw_materials: :costing_items} , :other_cost_fixeds, :other_costs, :exchange_rates ], :use_dictionary => true do |original, kopy|
                kopy.remote_picture_url = original.picture_url if kopy.is_a?(Image)
kopy.created_at = original.created_at
            end

然后它会在所有嵌套属性上添加created_at。

从这篇文章https://github.com/moiristo/deep_cloneable/issues/54

得到了这个答案