Embed + Sideload Relationships for a model
See original GitHub issueIs it possible to embed one relationship and sideload another?
For instance, imagine a situation like this:
"projects": [{
"id": "1",
"supervisor_id": "1",
"latest_metric": {
"id": "3",
"completed": "0.2"
}
}, {
"id": "2",
"supervisor_id": "1",
"latest_metric": {
"id": "5",
"completed": "0.5"
}
}],
"supervisors": [{
"id": "1",
"first_name": "Bob",
"last_name": "Jones"
}]
In this case, I sideload the supervisor because Bob is the supervisor of multiple project, so I don’t want to embed his info in each project
payload. However, latest_metric
is a distinct model, but unique to a project and embedded.
Currently, it looks like embed: true
can only be specified at the serializer level. Is that assumption correct? Should we allow a user to specify this?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:5
Top Results From Across the Web
Embed + Sideload Relationships for a model #633 - GitHub
In this case, I sideload the supervisor because Bob is the supervisor of multiple project, so I don't want to embed his info...
Read more >Ember: Access sideloaded model relationship data in template
I am trying to display a model's relationships in a template from sideloaded data, but there seems to be some problem. With Ember...
Read more >Customizing Serializers - Models - Ember Guides
Sideloaded Data. Data that is not a part of the primary request but includes linked relationships should be placed in an array under...
Read more >Serializer - Mirage JS
Set whether related models should be embedded or sideloaded. Doesn't apply to JSONAPISerializer. By default this false, so relationships are sideloaded:
Read more >Ember Data: A Tutorial and Examples of the Ember.js ... - Toptal
Sideloading ; Links; Active Model Serializer and Adapter; Embedded Records Mixin; Association Modifiers (Async, Inverse, and Polymorphic); 'ids' Parameter in GET ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I might add a +1 for a pick and choose which relationships are embedded/vs sideloaded vs just serialized Ids but this thread helped me write a serializer to fit my needs for now
@samselikoff that approach does work, but there there is a minor tweak to be done, at least in my case calling
Serializer.prototype.serialize.apply(this, arguments)
was not doing the actual serialization on my child object (in this case latestMetric, so its attributes were not converted to snake case). This resulted in the association working, but some attributes were invalid (I took a while to figure that out, it was definitely not my best programming moment).The tweak:
Thanks for the help and for
ember-cli-mirage
. If you think it might be worth to enhance theembed
option per association I can find some time to work on that, it would be a pleasure.