title: Nesting databases and using the data
body: <p>I'm new to rails, 2nd week at it actually - I'm in an academy, and I've hit a wall.</p>
  
  <p>I have 2 databases, posts and comments ... comments belongs_to post and post has_many comments.</p>
  
  <p>I've gotten my last 10 posts to list and I've gotten comments to list out underneath them.</p>
  
  <p>My question is I think one of logic.  How should I call the data so my comments will list under their respective posts?  I think I'm close.  I have them listing out for posts but they are listing in the wrong places.  I can't seem to figure out how to get them to the right place. </p>
  
  <p>Along with a solution should you provide one, please share with me your thought process on getting the answer.  </p>
  
  <p>I drew number charts and I believe I figured out that when the Post.user_id is equal to the User.id I'm posting on (I think that's right, still a little confused), that should be when to drop the comment? </p>
  
  <p>My code originally looked like this in the index.html.erb of Posts:</p>
  
  <pre><code>&lt;tbody&gt;
  &lt;% @posts.last(10).each do |post| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= post.body %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= post.user_id %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to 'Show', post %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to 'Edit', edit_post_path(post) %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>
  </p>
  
  <pre><code>&lt;ul&gt;
  &lt;h2&gt; Followed Posts &lt;/h2&gt;
  &lt;% current_user.followeds.each do |user| %&gt;
    &lt;% user.posts.each do |post| %&gt;
      &lt;li&gt;&lt;%= post.body %&gt; | &lt;%= post.user_id %&gt;&lt;/li&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;ul&gt;
  &lt;h2&gt; Comments &lt;/h2&gt;
  &lt;% @posts.each do |post| %&gt;
    &lt;% post.comments.each do |comment| %&gt;
      &lt;li&gt;&lt;%= comment.body %&gt; &lt;%= comment.id %&gt; &lt;%= comment.user_id %&gt; &lt;%= comment.post_id %&gt;&lt;/li&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p> </p>
  
  <p>I have been trying something like this now for the comments - it's actually for a different project but the same setup.  I did the above part once again to try to solve the problem and get more practice ... I'm at the same place with the same goal for both projects right now:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;% @user.posts.each do |post| %&gt;
          &lt;li&gt;
              &lt;%= post.title %&gt;&lt;br&gt;
              &lt;%= post.body %&gt;&lt;br&gt;
          &lt;!-- if there is a comment for a post - post it here --&gt;
          &lt;!-- if there is a comment for the Post_id, Output it here --&gt; 
                  &lt;% post.comments.each do |comment| %&gt;
                      &lt;%= comment.body %&gt; &lt;%= comment.id %&gt;
                      &lt;br&gt;
                  &lt;% end %&gt;
  
          &lt;div class="post-link"&gt; 
          &lt;%= link_to "Comment", new_post_comment_path(post) %&gt; |
          &lt;%= link_to "Edit", edit_post_path(post) %&gt; |
              &lt;%= link_to "Delete", post_path(post), method: :delete, data: { confirm: "Confirm you want to delete."} %&gt; 
        &lt;/div&gt;
    &lt;/li&gt;
      &lt;%end%&gt;
  </code></pre>
  
  <p></p>
  
  <p>I posted a little of my thought process commented out in the code.<br>
  Like I said, something wrong in my logic I believe since the comments are in the wrong place. 
  No particular pattern though.</p>
  
  <p>Output is like this:</p>
  
  <p>4
  Best new PS4 games - post.title
  The Last of Us, Battlefield 4, Assassin's Creed 4, in that order. - post.body</p>
  
  <p>---many comments end up jumbled below - comment.body and comment.id</p>
  
  <p>it does work, kind of
  yes! Apps? I love apps! Post number 2 here for Nate comment 3 for Nate 1 More sunglass comments Commenting on my own comment</p>
  
  <p>4
  High school is boring - post.title</p>
  
  <p>I hate having to go to school everyday. All I really want to do is sleep in as long as I want and then play video games. I think I'm going to be a professional video game player someday. There is such a thing as that, isn't there.  -  post.body</p>
  
  <p>--- many comments jumbled below - comment.body and comment.id</p>
  
  <p>sunglasses? Assassins creed! Hunting is cool. I hunted in florida I hate hs too</p>
  
  <hr>
  
  <p>So still trying to clarify the output:</p>
  
  <p>It posts the Post.body</p>
  
  <p>But then it may or may not post comments that should be assigned to this post.</p>
  
  <p>Another Post</p>
  
  <p>Comments that refer to this post and others end up here.</p>
  
  <p>Another Post</p>
  
  <p>Comments that belong to different posts end up here.
  (so every comment is not where it should be. Maybe my loop is not set up right and in the wrong place but I've tried a lot so far to get the right output)</p>
  
  <hr>
  
  <p>Well, I noticed for comments, the user_id and post_id are both identical as well 1 1 or 2 2, is that right? </p>