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.

Difficulty defining a "one-sided" belongsTo relationship

See original GitHub issue

First off, thank you for taking the time to help me with all of these questions. I’m really struggling to wrap my head around the new model/serialization structures.

I’m trying to set up this sort of behavior:

I have two models: User and Metric. Metric belongsTo a User, but User doesn’t include the metrics_id. For instance, the payload I’m wanting Mirage to send down is something like this:

// GET /user/1

{
  "user": {
    "id": 1,
    "name": "Foo"
  },
  "metrics": [
    {
      "id": "2016-04-06T16:07:58.166Z",
      "user_id": 1
    }
  ]
}

Because of some complexities around the generation of the metric id, it’s desirable for the server team to not include the metrics_id on the user payload.

Is there a way to do this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
samselikoffcommented, Jan 23, 2017

The ORM improvements in the 0-3 series should address this

www.ember-cli-mirage.com/blog/2017/01/09/0-3-0-beta-series/

1reaction
samselikoffcommented, Apr 8, 2016
  • one-to-one relationships aren’t really supported yet. You can fake it at the serializer layer. I would make one side hasMany then munge the data in serializer#serialize

  • your UserSerializer says include: ['metrics'] but the user doesn’t have any relationships under the key of metrics… it has a metric (singular). So the includes here are pointing to named associations.

  • Even if you correct metrics to metric it still won’t work. Let me explain why. Right now since 1-1 relationships aren’t supported, belongsTo assumes the child owns the fk. So, when you put metric: belongsTo() in the User model, you’re telling Mirage that a user has a metricId foreign key, and that’s how it looks up the attribute. But in the scenario, you create a metric with a userId. So when the serializer asks the user if it’s associated metric model exists, it gets null.

    You can either switch the scenario around so you first create the metric and then create the user passing in the { metric }, or turn user.metrics into a hasMany - but then you’d have to do some munging at the serializer layer.

Does that clear things up? I know this is frustrating/broken right now, I have hasOne built at the orm layer but I need to update the serializer layer to support it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

One-Sided Relationships: 24 Signs, Causes & Ways To Fix It
"A one-sided relationship can be defined as a relationship that lacks balance and equitable reciprocity. A relationship that lacks balance or ...
Read more >
Laravel: belongsTo() relation assume one-to-many ...
You need to define the relationship. You can define 'different' relationships on the perspective. The ->belongsTo() function is an inverse ...
Read more >
How to Tell If You're In a One-Sided Relationship
A one-sided relationship can be defined as an imbalanced interpersonal relationship where one person invests more energy or where one person ...
Read more >
Difficulty defining relationships · Issue #687 · miragejs/ember ...
Am I still able to load /assets/ that has a one-to-one belongsTo relationship with /assets/:id/images and have it fetch the belongsTo image ?...
Read more >
An Expert Explains the Psychology Behind a One-sided ...
Campbell explains that a one-sided relationship involves one person investing much more time and energy (and, in some cases, money) into the ...
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