I'm working on expanding a simpledb adapter, the code for it is here
(http://github.com/gigq/dm-adapter-simpledb/tree/master).  Whenever I
try to setup a one to many relationship between two models I run into
this "undefined method `items_association' for nil:NilClass".  This
occurs when wishlist.items is called from inside my code (wishlist
having n items and items belong to wishlist).

I've looked through the datamapper code as to why it's happening and
wanted to post here to find out what should be happening.

The code in question is in /dm-core-0.9.11/lib/associations/
relationship.rb line 97.  Here is the link directly to the file on
github (http://github.com/datamapper/dm-core/blob/
2c82ee9d009bb3f292308f2a067350282dc3a00f/lib/dm-core/associations/
relationship.rb)

Here is the code starting with the start of the function down to here
the error occurs:

      def get_children(parent, options = {}, finder = :all, *args)
        parent_value = parent_key.get(parent)
        bind_values  = [ parent_value ]

        with_repository(child_model) do |r|
          parent_identity_map = parent.repository.identity_map
(parent_model)

          query_values = parent_identity_map.keys

          bind_values = query_values unless query_values.empty?
          query = child_key.zip(bind_values.transpose).to_hash

          collection = child_model.send(finder, *(args.dup <<
@query.merge(options).merge(query)))

          return collection unless collection.kind_of?(Collection) &&
collection.any?

          grouped_collection = {}
          collection.each do |resource|
            child_value = child_key.get(resource)
            parent_obj = parent_identity_map[child_value]
            grouped_collection[parent_obj] ||= []
            grouped_collection[parent_obj] << resource
          end

          association_accessor = "#{self.name}_association"

          ret = nil
          grouped_collection.each do |parent, children|
            association = parent.send(association_accessor)

The problem I have is that my child value is this:

["axh3n0h9vjy5ispy34pfyg9or"]

Which is a base36 encoded uuid and the primary key of my model in
simpledb.  However my parent_identity_map is this:

#<DataMapper::IdentityMap:0x2552b78
 @cache=
  {[["axh3n0h9vjy5ispy34pfyg9or"]]=>
    #<Wishlist
     title = "blah",
     id = "axh3n0h9vjy5ispy34pfyg9or",
     created_at = #<DateTime: 4713648517/1920,-1/6,2299161>>},

As you can see it doesn't return back the parent object because the
identity map is keyed off of an array of arrays,
[["axh3n0h9vjy5ispy34pfyg9or"]] instead of the key it is using for the
lookup ["axh3n0h9vjy5ispy34pfyg9or"].

Which one is wrong here?  It looks to me like the identity map is
wrong but I've followed the code from where it is getting set and I
can't figure out how it could ever be anything different.  Same goes
with the child_value.

If I change the code on 97 to "parent_obj = parent_identity_map
[[child_value]]" then everything works fine.

If I look at it from the other end where identity map is set in
model.rb line 385 (http://github.com/datamapper/dm-core/blob/
2c82ee9d009bb3f292308f2a067350282dc3a00f/lib/dm-core/model.rb):

      if key_values && identity_map
        identity_map.set(key_values, resource)
      end

I can change this code to not set the identity_map with the array and
instead use the first value of the array.

      if key_values && identity_map
        identity_map.set(key_values.first, resource)
      end

This also results in the error going away.  The problem I'm having is
I'm sure there is a problem in the adapter somewhere that is causing
this but I'm lost as to how to figure out what it would be.  Any help
would be appreciated.  Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to