Reputation: 407
So i have these models
App.Order= DS.Model.extend({
items: DS.hasMany('item')
});
App.item=DS.Model.extend({
order: DS.belongsTo('order')
});
my question is, where shall i put the property quantity? Im sure this following codes are incorrect, because two orders can have similar items with different quantity, yet i can't think of any possible solution.
App.Order= DS.Model.extend({
items: DS.hasMany('item')
});
App.item=DS.Model.extend({
order: DS.belongsTo('order'),
quantity: DS.attr('number')
});
thanks for your help.
Upvotes: 0
Views: 84
Reputation: 407
Pedrocheckos answer is correct. It was really helpful.
i brought it for a discussion with my backend team, and we agreed to have 2 separate item models. instead of having many to many relationship model.
1 item model is just for display purposes, no quantity property
and the other item model is the property of order model, which will have quantity.
Upvotes: 0
Reputation: 3781
On a relational standpoint, your case is actually "many-to-many":
In your database, the quantity will likely belong to a table such as "Order_Item" where you will find both the order id and the item id (composite unique constraint).
Since you need to have data (the quantity at least) living in that table, you probably want to have an Ember model representing it (let's say OrderItem or OrderRow). Since your table will have its own primary key, you could design you rest API with classic "one-to-many" relations:
/orders
and /orders/:id
/items
and /items/:id
/order_items?order_id=:order_id
and /order_items/:id
And your models such as:
Helpful link: http://www.toptal.com/emberjs/a-thorough-guide-to-ember-data#one-to-many-and-many-to-one-relationships
Upvotes: 1