Hi, I struggled through so many posts here and on stackoverflow without any luck.
App.ApplicationAdapter = DS.ActiveModelAdapter.extend()
I have a simple relationship between two models:
App.Order = DS.Model.extend(Ember.Validations.Mixin,
transportationCosts: DS.attr("number")
...
items: DS.hasMany("order-item")
)
App.OrderItem = DS.Model.extend(
quantity: DS.attr("number")
...
order: DS.belongsTo("order")
item: DS.belongsTo("item")
)
App.Item = DS.Model.extend(
...
Now I fill up the order with orderItems like this.
orderItem: (item) ->
o = @content
itemAlreadyInOrder = false
o.get('items').forEach (oi) ->
if oi.get('item') is item
itemAlreadyInOrder = true
oi.set 'quantity', io.get('quantity')+1
if itemAlreadyInOrder is false
oi = o.get('items').createRecord 'order-item'
oi.set 'quantity', 1
oi.set 'item', item
That works well. I think all relationships are looking good.
Now I want to save that order, including it’s orderItems to my Rails backend. First, how is this officially done with ember-data? By first saving the child to the server and then saving the parent? How would this be implemented on the rails side? Should it save the record and wait for the parent and regularly clean the db from incomplete records?
My idea was, that both the parent with it’s embedded children could be posted to the server in one POST, with embedded children. looking like
{
"order": [
{
"transportationCosts": 20,
"order-items": [
{
"quantity": 2,
"item_id": 85
}
],
},
],
}
I know, this is not how records are embedded normally by ember. On order you would have a item_ids array and beside the order key in the root hash you would have a items key listing the items. Thats how I receive records from rails. But because the id’s are not yet known when posting this type of embedding doesn’t work. So how to do it? Do I have to write a specific serializer? Or stick with the official way, and which would this be?
Thank you very much for all your help!!