Describing a particular model:
#order/model.coffee
Order = DS.Model.extend {
line_items: DS.hasMany 'product', {async: true}
}
When attempting to add products to the order, it was discovered that adding the same product multiple times does not work as expected:
#product/route.coffee
...
actions:
# Example code illustrating the issue
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 challenge arises when users need to include multiple instances of the same item in an order. Unfortunately, Ember does not seem to allow duplicate entries in relationships. How can models be added more than once to a relationship?