Ruby on Rails deep copy/ deep clone of object and its attributes

前端 未结 2 1435
刺人心
刺人心 2021-02-02 13:20

I would like to do a deep copy on objects including all the attributes.

The experiment_old is has 10 trials. And I want to copy everything to experiment_new with the 10

2条回答
  •  深忆病人
    2021-02-02 13:50

    You should clone every trial and assign them to the cloned experiment:

    @experiment_new = @experiment_old.clone
    @experiment_old.trials.each do |trial|
      @experiment_new.trials << trial.clone
    end
    

提交回复
热议问题