Is this a good Ruby copy-constructor?
def copy_constructorThe model I am copying is "Course" and it has a N-M relation to a "Trainer" model. I use the "self.attributes" call, to set attributes on the new instance, but the relations are not copied. Also, I set "id" to nil, to make ActiveRecord think it is transient, which makes ".save" do an insert.
course = Course.new(self.attributes)
course.id = nil # make copy transient
# the "trainers" n-m relation is not copied in "new", as it is not an attribute on the model
course.trainers = self.trainers
course.trainer_ids = self.trainer_ids
return course
end
Any better way to do this?
1 comment:
thanks
renovationdoctors.com
turizmseyahat.blogspot.com
www.yagmurunsesi.org
yagmurunsesiorg.blogspot.com
turkuntarihi.blogspot.com
websitesiyapamak.blogspot.com
saglik-k.blogspot.com
ders-hane.blogspot.com
Post a Comment