1

Why can't I push a record multiple times when using has_many?

class Template
  include Mongoid::Document

  has_and_belongs_to_many :widgets, inverse_of: nil

Attempt to add duplicates:

(rdb:387) self.widgets.push(Widget.first)
[BSON::ObjectId('4f7096776c51c8135000000d')]

(rdb:387) self.widgets.push(Widget.first)
[BSON::ObjectId('4f7096776c51c8135000000d')]

(rdb:387) self.widgets.count
1
shingara
  • 46,608
  • 11
  • 99
  • 105
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114

1 Answers1

0

Internaly, Mongoid use the key $addToSet to put your new document associate. So this keyword avoid duplication of element in the list. So you can't have several times the same document associate with has_and_belongs_to_many.

Do a pull request or a feature request if you want this behavior. But I think it can be an option to allow this.

shingara
  • 46,608
  • 11
  • 99
  • 105