title: Rails Query to get all Posts in Forum Topic and then User for Each Post
body: <p>Please bear with a Ruby on Rails noob who is trying to create a first original application.</p>
  
  <p>Here are my associations:</p>
  
  <pre><code>class Forum
    has_many :topics
    has_many :posts
  end
  
  class Topic
    has_many :topics
    belongs_to :forum
  end
  
  class User
    has_many :posts
  end
  
  class Post
    belongs_to :user
    belongs_to :topic
  end
  </code></pre>
  
  <p>I want to make a query for a Topic show page that first gets all Posts in that Topic; they have <code>topic_id</code> as a foreign key to tie them to the Topic. I have successfully gotten that far with:</p>
  
  <pre><code>@topic = Topic.find(params[:id])
  @posts = Post.where("topic_id = ?", @topic.id)
  </code></pre>
  
  <p>I then want to get each User for each of these Posts by using the foreign key user_id that is in each Post’s record. I wondered if somehow I should nest attributes but just cannot figure out the proper Rails  syntax to then get each Post’s User.</p>
  
  <p>The final Topic show view would have something like:</p>
  
  <p>Topic #1<br>
     &nbsp;&nbsp;Post #1 Title<br>
     &nbsp;&nbsp;Post #1 Body<br>
     &nbsp;&nbsp;Posted by User.username at Post.created_at<br></p>
  
  <p>&nbsp;&nbsp;Post #2 Title<br>
     &nbsp;&nbsp;Post #2 Body<br>
     &nbsp;&nbsp;Posted by User.username at Post.created_at<br></p>