I\'m having trouble determining the correct way to persist a hasMany association with a single form using Ember.js, Ember Data and Rails. A Client hasMany Projects. I have a new
@David i am no expert but i was looking at your question again and i think to get the associated client_id, you will have to call toJSON method on the RestAdapter and pass it:
{associations: true}
The links below explain how to do that:
https://stackoverflow.com/questions/10184213/is-there-a-way-to-post-embedded-objects-back-to-the-api-with-ember-data
Ember-data embedded objects stored as separate objects
Emberjs - unable to query for embedded model or association
Update
Before i answer the question of where in the code to use toJSON, i want to go one step back to something i just observed with the design of your code.
You are trying to create a parentRecord within child record because Client is the parent while project is the child. If you observe well, issue-115 that you linked to, categorically says that the parentRecord store would be used to create the record. Also if you examine the test in evenutual pull-request that was merged in respect to issue-115, you will see it again stated as Create a child record within the parentRecord.
But you are doing the reverse, that is creating parentRecord within child record. You need to reverse that.
Also, on the rails side, in the Client Model, you called accepts_nested_attributes_for :projects, which just like emberjs does only allows you to create project records from Client and not vice-versa. All the examples of how to use accepts_nested_attributes_for in the rails guide, railscasts and this on nested attributes, show the fields for the child model created from the forms in the parent model.
So both rails and emberjs are in this use case to create parentRecord from child record. This could be why you having the client-id issue. So reverse your design and try it again. If it still doesn't send the client-Id. Then you should called toJSON on the Client Model which is the parent association as shown in the first link from the initial links i posted recommending you call {associations: true} on toJson in the restAdapter.
App.Client
##
toJSON: (options={}) ->
options['associations'] = true
@_super(options)
I hope this helps. Kindly comment back on what worked or did not work, so that others experiencing similar issues, who find this post can know where to start from.
Finally, it wouldn't hurt to create your rails api with active_model_serializer gem, which is ember-core team's recommended gem for creating rails api for ember-data and the RestAdapter. That way you can sideload and embed associations both from the ember side and the rails side.