Reputation: 1301
This one is a little bit tricky for me since it is not covered in documentation at all.
I have four models:
App.Dog = DS.Model.extend();
App.Cat = DS.Model.extend();
App.Food = DS.Model.extend();
App.Toy = DS.Model.extend();
Both dog
and cat
have many food
and many toys
. On the other hand a single instance of food
and toy
can belong only to one dog
or cat
(so it is hasMany
-ish relation on the animal side and belongsTo
-ish on the item side).
Any hints how to do this in Ember using polymorphic relations? Or maybe there is a better approach to achieve this other than polymorphic relations?
Also what code should I implement in my controller to create a new food
or toy
record that belongs to dog
or cat
?
Thanks!
Upvotes: 3
Views: 148
Reputation: 1286
Based on this slideshow, something like:
App.Animal = DS.Model.extend({
food: DS.hasMany('App.Food', { polymorphic: true }),
toys: DS.hasMany('App.Toy', { polymorphic: true })
});
App.Dog = App.Animal.extend(/* .... */);
App.Cat = App.Animal.extend(/* .... */);
App.Food = DS.Model.extend({
owner: DS.belongsTo('App.Animal', { polymorphic: true })
});
App.Toy = DS.Model.extend({
owner: DS.belongsTo('App.Animal', { polymorphic: true })
});
Upvotes: 1