mongoid/mongoid — pull request #3203 by DouweM (2013-08-03 16:18:24+00:00)
Title: Add correct index for indexed polymorphic belongs_to relations.

The index being created was `{ relation_id: 1, _type: 1 }`, which doesn't help us at all. We need `{ relation_id: 1, relation_type: 1 }`. It looks like this bug has been in place ever since this feature was added, over a year ago: #1081 / 5deb1d55aabff82316f546d449b0678962e259e7

`metadata.type` returns `#{as}_type` which does the trick for `has_many` side of a polymorphic relation, but for the `belongs_to` side, `as == nil`, thus having `metadata.type` evaluate to `_type`. On this side, we need `metadata.inverse_type`, which returns `#{name}_type`, which *is* what we want.