Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for the same inverse association key to exists many times #1305

Merged
merged 1 commit into from
Apr 19, 2018
Merged

Allow for the same inverse association key to exists many times #1305

merged 1 commit into from
Apr 19, 2018

Conversation

jrjohnson
Copy link
Contributor

While this solution works I don't really know enough about the mirage internals to know if it is the right fix.

Hopefully a test is worth a thousands words here, I'm finding this issue difficult to explain. This should be supported:

user: Model.extend({
  authoredPosts: hasMany('post', { inverse: 'authors' }),
  authoredBooks: hasMany('book', { inverse: 'authors' })
}),
post: Model.extend({
  authors: hasMany('user', { inverse: 'authoredPosts' })
}),
book: Model.extend({
  authors: hasMany('user', { inverse: 'authoredBooks' })
})

The user model here inverses two other models with the same property.

@@ -271,7 +271,7 @@ class Model {
let associations = this.schema.associationsFor(this.modelName);
let matchingExplicitInverses = Object.keys(associations).filter(key => {
let candidate = associations[key];
let modelMatches = association.modelName === candidate.ownerModelName;
let modelMatches = association.modelName === candidate.ownerModelName && association.ownerModelName === candidate.modelName;
Copy link
Collaborator

@samselikoff samselikoff Apr 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Miraculously, the existing code is simply wrong. Not sure how it managed to survive this long.

We're making sure there's only one association on user that matches post.authors, so we want to check all the associations on user (the candidates) whose modelName matches the given association's ownerModelName (and whose inverse key matches).

The code should be

let modelMatches = association.ownerModelName === candidate.modelName;

That should pass your new test (and all others).

Thanks for digging into this and opening a PR!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, it felt like that might be true to me, but I was completely unable to keep all the association and model names straight. I must have stared at my debugger for 15 minutes just looking.
Anyway, I just pushed an amended commit with only the correct check.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha I know... very confusing part of the codebase. Always open to improvements if you have any ideas!

Thanks!

@samselikoff samselikoff merged commit d10a979 into miragejs:master Apr 19, 2018
@jrjohnson jrjohnson deleted the multiple-matching-inverse branch April 19, 2018 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants