我正在复制一个 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
使用deep_clone
似乎很简单,并且可以在 do block 中完成,所以如果我们这样做
@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
我是一名优秀的程序员,十分优秀!