-
Type: Bug
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: 7.3.3
-
Component/s: Associations
-
None
-
Fully Compatible
As described in the summary:
class FireHydrant include Mongoid::Document field :location, type: String has_and_belongs_to_many :cats, primary_key: :name end class Cat include Mongoid::Document field :name belongs_to :person, primary_key: :username end > fire_hydrant = FireHydrant.create(location: '221B Baker Street') #<FireHydrant _id: 6174ccb84aec8bea5a39ba2a, location: "221B Baker Street", dog_ids: nil, cat_ids: nil> > fire_hydrant.cats.build name: 'Meow' #<Cat _id: 6174ce0a4aec8bea5a39ba2d, name: "Meow", person_id: nil> > fire_hydrant.cat_ids [BSON::ObjectId('6174ce0a4aec8bea5a39ba2d')] # instead of ['Meow'] > cats = [Cat.create(name: 'Kitty'), Cat.create(name: 'Kitten')] [#<Cat _id: 6174ccbc4aec8bea5a39ba2b, name: "Kitty", person_id: nil>, #<Cat _id: 6174ccbc4aec8bea5a39ba2c, name: "Kitten", person_id: nil>] > fire_hydrant.cats = cats [#<Cat _id: 6174ccbc4aec8bea5a39ba2b, name: "Kitty", person_id: nil>, #<Cat _id: 6174ccbc4aec8bea5a39ba2c, name: "Kitten", person_id: nil>] > fire_hydrant.cat_ids [BSON::ObjectId('6174ccbc4aec8bea5a39ba2b'), BSON::ObjectId('6174ccbc4aec8bea5a39ba2c')] # instead of ['Kitty', 'Kitten']
Some codes in Mongoid::Association::Referenced::HasAndBelongsToMany uses doc._id directly, regardless of the association's primary_key.