sundbp
4/8/2010 - 8:26 AM

one_to_one.rb

diff --git a/lib/dm-core/associations/many_to_many.rb b/lib/dm-core/associations/many_to_many.rb
index fe587cc..120d5f1 100644
--- a/lib/dm-core/associations/many_to_many.rb
+++ b/lib/dm-core/associations/many_to_many.rb
@@ -467,8 +467,17 @@ module DataMapper
           attributes = {}
           attributes[via] = resource if resource

-          intermediary = intermediaries.first_or_new(attributes)
-          return unless intermediary.__send__(method)
+          # note: it seems possible that through.get! sets intermediaries to a single resource.
+          # i'm not quite sure if that makes sense, but by checking for what type intermediaries is
+          # we can handle the case.
+          if intermediaries.is_a? DataMapper::Collection or intermediaries.is_a? DataMapper::Model
+            intermediary = intermediaries.first_or_new(attributes)
+            return unless intermediary.__send__(method)
+          elsif intermediaries.is_a? DataMapper::Resource
+            intermediary = intermediaries
+          else
+            intermediary = nil
+          end

           # map the resource, even if it is nil, to the intermediary
           intermediary_for[resource] = intermediary