mongoid/mongoid — pull request #2288 by dzello (2012-08-13 13:22:48+00:00)
Title: Match also on class name when looking up inverse.

Hit a snag when upgrading to Mongoid 3 with an app that makes good use of polymorphic models. Here's the setup:

```ruby
class User
has_many :notifications, as: :interest
has_many :icons, as: :interest
end

class Notification
belongs_to :interest, polymorphic: true
end

class Icon
belongs_to :interest, polymorphic: true
end
```

Setting `icon.interest = user` in Mongoid 3 causes an InvalidSetPolymorphicRelation exception. Mongoid is unhappy that `User` has two polymorphic relations called 'interest` and believes the assignment is ambiguous.

However, the situation isn't ambiguous if we look at the relation classes. Because we're assigning from the `Icon` class, we know which `interest` on `User` the assignment pertains to.

This patch adds logic to `Metadata#lookup_inverses` to only add a match when the inverse class and the potential match's class are equal.

I added a spec w/ a simliar scenario to this that fails due to the InvalidSetPolymorphicRelation exception w/ the old code and passes with the patch. I also had to update one line in the metadata spec which, though it passed before, I believe was conceptually a hair off and broke w/ the patch (as a false negative).