Include hasMany with fixtures does not seem to work
See original GitHub issueI 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:
- Created 8 years ago
- Comments:6
Top 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 >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
It now works with
ember-cli-mirage#master
, thanks!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:
and everything should work. I’ll open a new issue to improve this in the future. Thanks for reporting this!