Ember Data does not allow duplicate entries in hasMany relationships?

I have the following model:

#order/model.coffee
Order = DS.Model.extend {
  line_items: DS.hasMany 'product', {async: true}
}

At some point I want to add the some products to the order. I found that I can only add the product once, adding the same product again does not work:

#product/route.coffee
...
actions:
    # Not actually my code but illustrates the problem
    addToCart: (product1, product2)->
      order = @modelFor 'order'
      console.log order.get('line_items.length') # prints 0

      order.get('line_items').pushObject product1
      console.log order.get('line_items.length') # prints 1

      order.get('line_items').pushObject product2
      console.log order.get('line_items.length') # prints 2

      order.get('line_items').pushObject product1
      console.log order.get('line_items.length') # prints 2

      order.get('line_items').pushObject product2
      console.log order.get('line_items.length') # prints 2


      ...

The problem is that the user might want a single item more than once. The simplest way to represent that is to have an array with duplicate entries. It seems Ember is not letting me do that for relationships. How can I add a model more than once to a relationship ?

First asked, unsuccessfully, on SO here: javascript - Ember Data does not allow duplicate entries in hasMany relationships - Stack Overflow. There’s a nice bounty for you there if you find a solution!

1 Like