Difficulty defining a "one-sided" belongsTo relationship
See original GitHub issueFirst 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:
- Created 7 years ago
- Comments:8 (2 by maintainers)
Top 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 >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
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/
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 inserializer#serialize
your
UserSerializer
saysinclude: ['metrics']
but the user doesn’t have any relationships under the key ofmetrics
… it has ametric
(singular). So the includes here are pointing to named associations.Even if you correct
metrics
tometric
it still won’t work. Let me explain why. Right now since 1-1 relationships aren’t supported,belongsTo
assumes the child owns thefk
. So, when you putmetric: belongsTo()
in the User model, you’re telling Mirage that auser
has ametricId
foreign key, and that’s how it looks up the attribute. But in the scenario, you create ametric
with auserId
. So when the serializer asks the user if it’s associatedmetric
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 turnuser.metrics
into ahasMany
- 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.