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.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. 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. 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. And each other to respect their relations.
Thanks allot.