gpt4 book ai didi

ruby - deep_clone gem 不复制 created_at

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:12 24 4
gpt4 key购买 nike

我正在复制一个 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

关于ruby - deep_clone gem 不复制 created_at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34680742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com