Permalink
Browse files

Make sure DeepCloning works with belongs_to and has_one associations

  • Loading branch information...
1 parent dec89e6 commit f0dcbeb47bcd54b65e49efd56293f97450693c29 @DefV DefV committed Dec 18, 2008
Showing with 7 additions and 1 deletion.
  1. +7 −1 lib/deep_cloning.rb
View
@@ -44,7 +44,13 @@ def clone_with_deep_cloning options = {}
association = association.keys.first
end
opts = deep_associations.blank? ? {} : {:include => deep_associations}
- kopy.send("#{association}=", self.send(association).collect {|i| i.clone(opts) })
+ cloned_object = case self.class.reflect_on_association(association).macro
+ when :belongs_to, :has_one
+ self.send(association) && self.send(association).clone(opts)
+ when :has_many, :has_and_belongs_to_many
+ self.send(association).collect { |obj| obj.clone(opts) }
+ end
+ kopy.send("#{association}=", cloned_object)
end
end

0 comments on commit f0dcbeb

Please sign in to comment.