简体   繁体   English

Ruby on Rails克隆/复制带有过滤的嵌套元素

[英]Ruby on rails clone/dup with filtered nested elements

i have an issue in trying to clone an object that has nested elements. 我在尝试克隆具有嵌套元素的对象时遇到问题。 I know how to completely clone it, but i want to clone only some of the children elements on each nesting level. 我知道如何完全克隆它,但是我只想在每个嵌套级别上克隆一些子元素。

So lets say i have structure like this,im making by example names (its not actual case) 因此,可以说我具有这样的结构,即通过示例名称(并非实际情况)

class Town < ActiveRecord::Base
 has_many :districts
 has_many :buildings, through: :districts

class District < ActiveRecord::Base
 belongs_to :town
 has_many :buildings

class Building < ActiveRecord::Base
 belongs_to :district
 has_many :rooms
 has_many :toilets

 #and i have of course rooms and toilets with belongs_to relation to Building

So basicaly if i want to clone entire existing Town object i would just do something like 所以基本上,如果我想克隆整个现有的Town对象,我会做类似的事情

Town.last.clone

But i want to do something like this instead. 但我想做这样的事情。 I want to dup existing Town, and then recreate only some of the children objects, based on collections i have. 我想复制现有的Town,然后根据我的收藏仅重新创建一些子对象。 So if town has 10 districts, i want to dup only one of them, or more of them (based on collections i have), and then district has 100 houses, i will again dup only the ones i need and so on untill rooms and toilets. 因此,如果城镇有10个区,我只想复制其中一个,或者多个(根据我所拥有的收藏),然后一个区有100个房屋,那么我将再次仅复制我需要的房屋,依此类推,直到房间和洗手间。 So i tryed with something like this. 所以我尝试了这样的事情。

#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?

So i just want selective dup/clone of filtered elements belonging to parent. 所以我只想要选择性dup /克隆属于父级的过滤元素。 And each other to respect their relations. 互相尊重彼此的关系。

Thanks allot. 谢谢分配。

districts.each do |district|
  district_dup = district.dup
  buildings.each do |building|
    district_dup.buildings << building.dup
  end
  new_town.districts << district_dup
end

Could you try something like that? 你可以尝试类似的东西吗? If you want to add rooms and toilets, you will need to create building_dup just like district_dup . 如果要添加房间和卫生间,则需要像district_dup一样创建building_dup

Edit: Cleaner version 编辑:清洁版本

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

If dis.buildings = buildings.map(&:dup).each fails, please try to change #= to #concat in all cases. 如果dis.buildings = buildings.map(&:dup).each失败,请尝试在所有情况下将#=更改为#concat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM