question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Embed + Sideload Relationships for a model

See original GitHub issue

Is 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
sethphillipscommented, May 1, 2018

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

2reactions
juanazamcommented, May 12, 2016

@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:

export default ApplicationSerializer.extend({
  serialize(project, request) {
    let json = Serializer.prototype.serialize.apply(this, arguments);
    let latestMetricsAttrs = Serializer.prototype.serialize.apply(this, [project.latestMetric, request]);

    json.latest_metric = latestMetricsAttrs;

    return json;
  }
});

Thanks for the help and for ember-cli-mirage. If you think it might be worth to enhance the embed option per association I can find some time to work on that, it would be a pleasure.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found