Getting modifiers working as upserts using derived classes and single collection inheritance #454

Closed
kbullaughey opened this Issue Sep 22, 2012 · 2 comments

2 participants

@kbullaughey

I've found that when one does an upsert using one of the modifiers (increment, set, push, etc.) on a derived document class (i.e. using single collection inheritance), if the document doesn't exist, then the newly created document doesn't have _type set.

I've gotten around this by overriding criteria_hash() in MongoMapper::Plugins::Sci::ClassMethods as follows:

module MongoMapper
  module Plugins
    module Sci
      module ClassMethods
        # I override this method so that it works with upserts.
        def criteria_hash(criteria={})
          criteria = criteria.merge(:_type => name) if single_collection_inherited?
          super criteria
        end   
      end
    end
  end
end

The above as a gist

Is there any reason this isn't part of the present version? Or would this introduce any problems? If not, it would be nice to see this change added.

Cheers,
Kevin

@cheald
MongoMapper member

Can you provide a unit test for this behavior? I'm not suite sure how to replicate the issue.

@cheald cheald closed this in c7856e4 Jul 8, 2013
@kbullaughey

Sorry I didn't get back sooner with a spec. I can confirm that this fix solves my problem and the spec you added is equivalent to what I would have offered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment