I want to set parent_id in child records.
My models are Recipe (id, title) and Ingredients(id, about, recipe_id).
I have a form where i create recipe with many ingredients. All works well but i don’t know how to set a parent_id in child records after parent record is created.
Example:
We create recipe (id = 1, title = ‘Title01’). So all our ingredients will have field recipe_id with 1 (id=2, about=‘this is ingredient’, recipe_id=1)
recipe.js.coffee
App.Recipe = DS.Model.extend title: DS.attr('string') ingredients: DS.hasMany('App.Ingredient')
ingredient.js.coffeeApp.Ingredient = DS.Model.extend about: DS.attr('string') recipe: DS.belongsTo('App.Recipe')
new.emblem (here i create parent Recipe and children Ingredients)h1 Create recipe form div label{bindAttr for='title.elementId'} Title Ember.TextField valueBinding='title' name='title' viewName='title' div label{bindAttr for='about.elementId'} About Ember.TextArea valueBinding='about' name='about' viewName='about' #ingredients h3 Ingredients each ingredient in content.ingredients itemViewClass='App.ItemView' h3= this.view.rebasedIndex .control-group .controls Ember.TextField valueBinding='ingredient.about' a{ action removeIngredient ingredient target='controller' } href='#' Remove div a{ action addIngredient target='controller' } href='#' Add Ingredient button.btn.btn-success{ action save this } Create button.btn.btn-inverse{ action cancel this } Cancel
Here is my recipes_new_controller.js.coffee
App.RecipesNewController = Ember.ObjectController.extend save: -> @transaction.commit() cancel: -> console.log "cancel recipe" addIngredient: -> ingredients = @content.get('ingredients') ingredients.createRecord recipe: @content startCreating: -> console.log 'startCreating' @transaction = @get('store').transaction() @set 'content', @transaction.createRecord(App.Recipe, {})