Object cloning with relations

hello,
i have a problem with cloning objects.
the cloning of the objects is not really a problem.
i can do that like this:

def ActiveRecord::Base.clone(country_id)
cloneObj = self.find(:first, :conditions => “country_id = 1”)
newObj = self.new
newObj.save
newObj = cloneObj.dup
newObj.country_id = country_id
newObj.save
end

the problem is that i would like to generalize the cloning of the
has_and_belongs_to_many relations, too.

so instead of doing:

cloneObj.collection.each do |foo|
newObj.collection << foo
end

in each and every model that has collections i would rather do that on
an abstract level, too.

so i think my question is: is there a way to find out if the class has
has_many_and_belongs_to_many relations? and if so is there a way to use
that to assign the collections to the “cloned” objects as a general
extension of ActiveRecord::Base, like the clone method above?

thanks for looking into that :slight_smile:

kind regards,
alexander

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs