Ember data model explicit inverse relationships

318 views Asked by At

I’m struggling with explicit inverse relationships: https://guides.emberjs.com/release/models/relationships/#toc_explicit-inverses.

If 4 instances of the blog-post model can be associated with a comment (onePost, twoPost, redPost, bluePost), why is the inverse set on the class definition of the blog-post and not at an instance level?

Surely all instances that are created from that model definition will now be inverse to redPost? How do I create, say, a blog-post that I would like to be associated with the comment's bluePost attribute?

The closest answers I've found are Ember - Understanding Inverse Relationships and Setting the inverse option dynamically/conditionally in ember-data? but the first one oversimplifies the problem as to not be a relevant answer and the second one went unanswered.

Thanks in advance.

1

There are 1 answers

0
Don Omondi On

Yes, this one can be very confusing sometimes! But the shocking reality is that its very very simple :)

Here's my custom example : cars

If we used to drive from front to back how will you relabel the gears?

Simple, drive gear becomes reverse and reverse gear becomes drive!

With Ember Data the exact same principle applies. If you want Ember Data to know how to get from one model to another, just specify the field that holds the object. This key is the inverse.

What does this mean?

Any model can be an inverse of another, it just depends on what direction you want to go. Common practice is to travel from a blog article model to comments but you'd be correct to refer it the other way round if you want. In the end whether you drive forward or drive front-to-back in reverse, you'll get to the same place, just different conventions.

Inverse has no special functionality here other than helping Ember Data to traverse from one model to another.