mongoid/mongoid — pull request #3173 by ukisami (2013-07-18 06:40:32+00:00)
Title: Fix primary_key option on ManyToMany relations

Consider the following relations
```
class Car
include Mongoid::Document
field :vin, type: String
has_and_belongs_to_many :owner, primary_key: :ssn
end
```
```
class Owner
include Mongoid::Document
field :ssn, type: String
has_and_belongs_to_many :cars, primary_key: :vin
end
```

Queries looks up related objects by primary_key defined on the relation:
```
owner = Owner.create ssn: '1234'
car = Car.create vin: 'ABCD', owner_ids: ['1234']
car.owners # returns [owner]
```

However, inserts uses the primary_key on the base object instead
```
car = Car.create vin: 'ABCD'
owner = Owner.create ssn: '1234'
car.owners << owner # complains about :ssn field not defined on Car
```

The query and binding behaviors on ManyToMany relations are inconsistent. I think the query behavior is implemented intuitively, so this seems to be a bug on bindings.

Another issue is that car will save owner's ObjectID in its owner_ids set regardless of the primary_key field defined on either side of the relation.

This patch attempts to solve both of these issues. #2809 was used as a reference.