Reputation: 2190
I'm running into an issue of querying data against Ember data.
I have three models:
media
: which multiple models inherit from
image
: which inherits from media
note
: which is a standard model.
I'm trying to get all of the note
s of an image
but the query I'm trying isn't working.
// imageModel.js
import Ember from 'ember';
import DS from 'ember-data';
import MediaModel from 'models/mediaModel';
export default MediaModel.extend({
fileName: DS.attr('string'),
fileExt: DS.attr('string'),
url: DS.attr('string'),
});
// mediaModel.js
import DS from 'ember-data';
export default DS.Model.extend({
notes: DS.hasMany('note', { inverse: 'noteable' }),
});
// noteModel.js
import DS from 'ember-data';
import Ember from 'ember';
export default DS.Model.extend({
category: DS.attr('string'),
text: DS.attr('string'),
image: DS.belongsTo('image'),
noteable: DS.belongsTo('media-model', { polymorphic: true }),
});
Once I have an image
, I do image.get('notes.length')
, but even if an image does have note
s associated with it, I'm always getting back 0
. Am I querying this the wrong way?
Does the fact that image
belongs to media
affect how I can query the hasMany
of media
?
Thank you
Upvotes: 1
Views: 93
Reputation: 4005
Your code has a bug. Your hasMany-definition in mediaModel.js has an invalid model-name.
Change
notes: DS.hasMany('note', { inverse: 'noteable' }),
to
notes: DS.hasMany('note-model', { inverse: 'noteable' }),
Here's a working twiddle of the fixed code. Check it out.
Upvotes: 2