1
votes

So lets say a recipe has several ingredients of differing amounts.

Recipe Model

var Recipe = DS.Model.extend({
    name: DS.attr('string'),
    ingredients: DS.hasMany('ingredient')
});

Ingredient Model

var Ingredient = DS.Model.extend({
    name: DS.attr('string'),
    recipes: DS.hasMany('recipe'),
    // amount?
});

So the amount of each ingredient would depend on the recipe. However on its own the ingredient will not have an amount.

How would you go about modeling that data? Right now I am using the FixtureAdapter until I finish building the interface.

Using Ember 1.5.1 and Ember-Data 1.0.0-beta.7+canary.b45e23ba.

1

1 Answers

0
votes

To answer your first question:

Define the model like so

App.Comment = DS.Model.extend({
  message: DS.belongsTo('message', {
    polymorphic: true
  })
});

And the property needs an additional property propertyType, defining the relationship type

{
  "message": 12,
  "messageType": "post"
}

https://github.com/emberjs/data/blob/master/TRANSITION.md#polymorphic-relationships

Now to your second question, not sure if polymorphism would be necessary. I might just include a joining record

var RecipeIngredient = DS.Model.extend({
    amount: DS.attr(),
    ingredient: DS.belongsTo('ingredient')
});

var Recipe = DS.Model.extend({
    name: DS.attr('string'),
    ingredients: DS.hasMany('recipeIngredient')
});