title: How to Add Child Record to Existing Parent Record?
body: <p>I've been googling and scouring Stack Overflow for some sort of hint on this subject but the information is scattered at best.</p>
  
  <p>I'm trying to Create a new Child Record (<code>Comment</code>) and save it to an existing Parent Record (<code>Post</code>).  I am using Ember-Model, rather than Ember-Data, but any tips or pointers would be greatly appreciated.</p>
  
  <p>At the moment, I've been successful creating a new, embedded <code>Comment</code> but <strong>only</strong> when it is created with a <em>new</em> <code>Post</code> record.  So:</p>
  
  <p><strong>How do I go about loading/retrieving the currently loaded <code>Post</code>(parent record) in order to apply <code>Comments</code> (child records) to it?</strong></p>
  
  <p>I've been reading up on controller dependencies, using <code>needs:</code> and <code>this.controllerFor</code> and <code>this.modelFor</code> in order to have access to another controller/model's <code>content</code> but have been unable to wire these things together into something meaningful.</p>
  
  <p>Anyway, here is what I've whittled my application code down to, in the hopes I might be able to stumble into the proper way of doing this...</p>
  
  <h3>Routes</h3>
  
  <pre><code>    App.Router.map(function() {
        this.resource('post', { path: '/:post_id' }, function() {
          this.resource('comments', { path: '/comments'} );
          });
      });
  </code></pre>
  
  <p>I removed all the other resources &amp; routes, so I'm left with <code>App.Post</code>, <code>App.PostIndex</code>, and <code>App.Comments</code>.  I think my routes are the issue here, I assume I'm not properly implementing the methods to use the loaded <code>Post</code> record in my <code>Comments</code> route.</p>
  
  <pre><code>    App.IndexRoute = Ember.Route.extend({
        model: function() {
          return App.Post.find();
        },
  
        setupController: function(controller, model) {  // I'm not certain if this
          controller.set('content', model);           // setupController is needed?
        }
  
      });
  
      App.PostRoute = Ember.Route.extend({
        model: function(params) {
          return App.Post.find(params.post_id);
        },
  
        setupcontroller: function( controller, model) { // again, unsure if this
            this.controllerFor('post').get('comments'); // is correct.
              controller.set('content', comments);
        }
  
      });
  
      App.CommentsRoute = Ember.Route.extend({
        afterModel: function() {
            this.set('post', this.modelFor('post'));
          },
  
        setupcontroller: function( controller, model) {
            this.controllerFor('post').get('comments');
              controller.set('content', comments);
          }
  
      });
  </code></pre>
  
  <h3>Controller</h3>
  
  <pre><code>App.CommentsController = Ember.ArrayController.extend({
    needs: "post",
  
    actions: {
  
       addComment: function() {
            var post = App.Post.create({
              title: 'static post title'
            });
  
                post.get('comments').create({
                  message: 'static message'
              });
  
                post.save();
  
        }
  
    }
  
  });
  </code></pre>
  
  <p>This is my current Comments Controller, which can create a new <code>Post</code> with an embedded <code>Comment</code>.  I've found and been given numerous examples in which to create the <code>Comment</code>, but none seem to work for me.  Basically, I'm struggling with defining the <code>var post = ...</code> as the currently loaded record.  I've implemented various approaches in an attempt at trial &amp; error.  Thus far I have attempted:</p>
  
  <ul>
  <li><p><code>var post = App.Post.create();</code>, returns property undefined, as this would create a new record.  However, I gave it a shot as every example i saw related to this defined their record as such.</p></li>
  <li><p><code>var post = this.get('post');</code>, returns a cannot call 'get' on undefined.  I've tried using this method of defining my current post on both the <code>Comments</code> controller and <code>Post</code> controller.</p></li>
  <li><p><code>var post = this.get('controllers.post.content);</code>, returns a 'cyclic error' from the backend I'm using.</p></li>
  <li><p><code>var post = App.Post.find();</code>, returns a cannot call 'get' on undefined.</p></li>
  <li><p><code>var post = App.Post.find(1);</code>, Again, returns a cannot call 'get' on undefined.  Figured I'd give it a shot because this is one of those recurring examples people provide.  The backend I use applies its own ID to each record, and I'm unsure if I would be able to/how to have the <code>.find()</code> method use a dynamic ID value and retrieve only the model I just loaded.</p></li>
  </ul>
  
  <p>I'm guessing that I'm not properly setting up my Routes and Controller dependencies?</p>
  
  <p>If anyone has a suggestion, relevant link, or fix I would be very grateful.</p>
  
  <p>This one (seemingly simple) issue/use case has me at wit's end at this point.</p>