我正在复制一个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
答案 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。
得到了这个答案