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.

Include hasMany with fixtures does not seem to work

See original GitHub issue

I have something similar to:

mirage/models/user.js:

import { Model, hasMany } from 'ember-cli-mirage'

export default Model.extend({
  comments: hasMany('text')
})

mirage/models/text.js:

import { Model, belongsTo } from 'ember-cli-mirage'

export default Model.extend({
  author: belongsTo('user')
})

mirage/fixtures/user.js:

export default [
  { id: 1, username: 'topaxi' }
]

mirage/fixtures/texts.js:

export default [
  { id: 1, content: 'foo', authorId: 1 },
  { id: 2, content: 'bar', authorId: 1 },
  { id: 3, content: 'baz', authorId: 1 }
]

mirage/scenarios/default.js:

export default (server) => {
  server.loadFixtures('users')
  server.loadFixtures('texts')
}

mirage/config.js:

export default () => {
  this.namespace = 'api/v1'

  this.get('/v1/users')
}

Now if I’m requesting /api/v1/users/1?include=comments, I will only get the user without any comments/texts included. I’m not sure if I’m doing something wrong. Should I specify the one-to-many relationship only once? Do the one-to-many includes only work if I create the models / objects by hand?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
topaxicommented, Mar 21, 2016

It now works with ember-cli-mirage#master, thanks!

0reactions
samselikoffcommented, Mar 21, 2016

This is sort of a bug, sorry about the trouble it caused. It’s because hasMany tries to ensure there’s a foreign key on the child model, if it doesn’t exist. Because the fk is a named foreign key in this case, your child model ends up with two fks.

The solution is to name the inverse. Add this to your user model:

comments: hasMany('text', {inverseOf: 'author'})

and everything should work. I’ll open a new issue to improve this in the future. Thanks for reporting this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rails has_and_belongs_to_many is confusing me with ...
1 Answer 1 · Has and belongs to many defines the relationship in both directions. If you only need to see what genres...
Read more >
Complex data relationships using fixtures and/or factories #443
I've been unable to figure out how to support the following data model with Mirage fixtures or factories: Two models: Vehicles and Cars, ......
Read more >
Relationships - Mirage JS
Once you've defined your models, you can define relationships between them using the belongsTo and hasMany helpers. Each helper adds some dynamic methods...
Read more >
120Volt vs. 12Volt - What is the Difference? - Light It Right
Low voltage fixtures are much more attractive to look at, they tend to have a more aesthetic look to them. The fixtures often...
Read more >
One-to-Many Associations | Ruby on Rails - Human-SE Lab
Each Quiz object may have association links with certain Question objects (i.e. ... Thus, Rails will be able to use the foreign key...
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