In my app on the server I have a many-to-many relationship. To simplify let us say I have a Post model and want to add different Category models to it. Categories in this example of course can have many posts.
When I save a post I want Ember Data to send the ids of the categories the user chose. If I understand it correctly, this is not currently possible with Ember Data as it a) does not support many-to-many relationships in this way and b) only sends ids for belongsTo relationships.
I searched a bit and found this solution on SO: ember.js - Save foreign key to other Model with hasMany relation - Stack Overflow
This looked a lot like what I wanted to achieve, with one caveat: It will always send ids of hasMany relationships. In my app I only want this to happen for the Post model.
So I thought about making this configurable and quickly thought of active_model_serializers which allows you to embed either full records or just ids.
Ember Data already allows embedding of full records when a mapping with embedded: 'always'
exists. I enhanced the solution from the SO answer above, adding a new parameter embedded: 'ids'
. Ids only get serialized now if this mapping exists.
This works fine, but leaves me with a heavily customized serializer in my app for a use case that I consider quite common.
This leads to my questions:
- I know that better support for many-to-many relationships is in planning. Is there anything on the horizon for my use-case or is there something already in place that I missed?
- If not, should I try to port back my changes to Ember Data and file a pull-request?
- While researching this issue, I found a third possible parameter in the code:
embedded: 'load'
. This seems to behave mostly likeembedded: 'always'
with some subtle differences I do not fully understand. What is ‘load’ meant for and what exactly is the difference to using ‘always’? - Is there a place where all possible options for key mappings are documented?