Skip to content

Create associate items #216

@MrSwitch

Description

@MrSwitch

With the autoFetch attribute, nested items are retrieved from associated tables.

Is it possible to have the same work for post, e.g if i were to post

  Person.create([{
     name:"John", 
     pets:[
        {name:"Fido"},
        {name:"Tigger"}
     ]
  }], function(){... respond when done...});

It would write items to the model which was associated with 'pets'

I found a work around using afterCreate hook e.g.

  afterCreate : function(){
      // Loop through and insert any children
      for(var x in this){
        if( this.hasOwnProperty(x) && this[x] instanceof Array && x in models){

          for(var i=0;i<this[x].length;i++){
            this[x][i]['owner_id'] = this.id;
          }

          models[x].create(this[x], function(err){
            if(err){
              console.log(err);
            }
            else{
              console.log("Added "+i +" pets");
            }
          });
        }
      }
 }

However i'd like it to be a little more abstract and have it know the reference to the child in question, e.g. not have to explicitily define it like i do with... this[x][i]['owner_id'] = this.id;

Also i'd only like it to trigger the callback if this has been completed successfully. But there is no "next" parameter passed to this particular hook.

Thanks in advance, great project.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions