Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails克隆/dup,带有过滤的嵌套元素_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails RubyonRails克隆/dup,带有过滤的嵌套元素

Ruby on rails RubyonRails克隆/dup,带有过滤的嵌套元素,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在尝试克隆具有嵌套元素的对象时遇到问题。我知道如何完全克隆它,但我只想克隆每个嵌套级别上的一些子元素 让我们假设我有这样的结构,我通过示例命名(不是实际情况) 但是我想做一些像这样的事情。我想复制现有的城镇,然后根据我拥有的收藏,只重新创建一些子对象。所以,如果镇上有10个区,我只想复制其中一个,或者更多(根据我收集的资料),然后这个区有100所房子,我会再次复制我需要的房子,直到所有的房间和厕所。所以我试过这样的方法 #now i have these collections, i get

我在尝试克隆具有嵌套元素的对象时遇到问题。我知道如何完全克隆它,但我只想克隆每个嵌套级别上的一些子元素

让我们假设我有这样的结构,我通过示例命名(不是实际情况)

但是我想做一些像这样的事情。我想复制现有的城镇,然后根据我拥有的收藏,只重新创建一些子对象。所以,如果镇上有10个区,我只想复制其中一个,或者更多(根据我收集的资料),然后这个区有100所房子,我会再次复制我需要的房子,直到所有的房间和厕所。所以我试过这样的方法

#now i have these collections, i get them in some fashion, and they belong to Town(1) below    
districts = District.find(1,2,3) #those ids are belonging to Town
buildings = Building.find(1,2,3) #those ids are belonging to districts above
rooms = Room.find(1,2,3,4,5,6) # and are belonging to some of buildings above
toilets = Toilet.find(1,2,3,4,5,6,7) #and are belonging to some of buildings

#so now i want to do this
old_town = Town.find(1)
new_town = old_town.dup.tap

districts.each od |district|
  new_town.districts << district.dup
end
#and i get all the districts to new created town but when i try further in nesting it wont work, like this

districts.each od |district|
  new_town.districts << district.dup

  buildings.each do |building|
    district.buildings << building.dup
  end
end

#and so on inside it for toilets and rooms it just wont work, like it doesnt attach them to new_town. What am i doing wrong?
#现在我有了这些收藏品,我以某种方式得到了它们,它们属于下面的城镇(1)
地区=地区。查找(1,2,3)#这些ID属于城镇
建筑物=建筑物。查找(1,2,3)#这些ID属于上面的地区
房间=房间。找到(1,2,3,4,5,6)#并属于上面的一些建筑
厕所=厕所。查找(1,2,3,4,5,6,7)#并属于某些建筑物
#所以现在我想这样做
old_town=town.find(1)
new_town=old_town.dup.tap
地区。每个od |地区|
新市镇

如果
dis.buildings=buildings.map(&:dup)。每个
都失败,请尝试在所有情况下将
#=
更改为
#concat

是否将建筑的副本放在属于新城的地区?如果是,这是:
district.buildings您好,是的,我正在尝试添加建筑物副本,以便将其附加到我创建的区域副本。现在我有点困惑,我知道你在说什么,但不知道如何在同一个循环中实现。至于厕所和房间,我也是这样做的,对吗?我明天会试试,但我认为这就是问题所在。你说得对。我很笨--我想知道为什么它不连接它们。。。我会把它标为正确答案,因为它是99%
Town.last.clone
#now i have these collections, i get them in some fashion, and they belong to Town(1) below    
districts = District.find(1,2,3) #those ids are belonging to Town
buildings = Building.find(1,2,3) #those ids are belonging to districts above
rooms = Room.find(1,2,3,4,5,6) # and are belonging to some of buildings above
toilets = Toilet.find(1,2,3,4,5,6,7) #and are belonging to some of buildings

#so now i want to do this
old_town = Town.find(1)
new_town = old_town.dup.tap

districts.each od |district|
  new_town.districts << district.dup
end
#and i get all the districts to new created town but when i try further in nesting it wont work, like this

districts.each od |district|
  new_town.districts << district.dup

  buildings.each do |building|
    district.buildings << building.dup
  end
end

#and so on inside it for toilets and rooms it just wont work, like it doesnt attach them to new_town. What am i doing wrong?
districts.each do |district|
  district_dup = district.dup
  buildings.each do |building|
    district_dup.buildings << building.dup
  end
  new_town.districts << district_dup
end
districts.map(&:dup).each do |dis| #creates an Array of duplications and iterates through elements
  dis.buildings = buildings.map(&:dup).each do |building| #creates an Array of duplications which is later added to dis.buildings
    building.rooms = rooms.map(&:dup)
    building.toilets = toilet.map(&:dup)
  end
end