title: Node.js w/ Bookshelf; set values for a many-to-many relation with an array of IDs
body: <p>I'm using Node.js with Bookshelf to setup an API. The JSON coming in the request looks like this</p>
  
  <pre><code>{
    conversation:  {
      user_ids: [1, 2, 3, 4]
    }
  }
  </code></pre>
  
  <p>The conversation object looks like this:</p>
  
  <pre><code>var Conversation = storeManager.bookshelf.Model.extend({
    tableName: 'conversations',
  
    users: function() {
      this.belongsToMany(User)
    }
  
  })
  </code></pre>
  
  <p>The user looks like this:</p>
  
  <pre><code>var User = storeManager.bookshelf.Model.extend({
    tableName: 'users',
  
    conversations: function() {
      return this.belongsToMany(Conversation)
    }
  })
  </code></pre>
  
  <p>The database has a conversations and users table along with a conversations_users table that has a <code>user_id</code> and <code>conversation_id</code> field.</p>
  
  <p>So I've been going through the docs and I'm struggling to figure out how I can create a conversation object that has a relationship with the existing users with the given IDs.</p>
  
  <p>Here is what I have in my controller so far</p>
  
  <pre><code>ConversationController.prototype.create = function(req, res, next) {
    // Create the conversation
    Conversation.forge().set('users', req.body['conversation']['user_ids']).save().then(function(model) {
      req.conversation = model
      next()
    }).catch(function(err) {
      res.status(400).send(err)
    })
  }
  </code></pre>