title: ActiveRecord Associations: Getting data through multiple tables
body: <p>I am having a problem with my Active Record Associations and was wondering if someone could point me in the right direction. I will try the best to explain my problem, but I'm not even sure what to title it. </p>
  
  <p>I have three tables in my database that are all interconnected and I need to call information from each of the three tables within one view file. </p>
  
  <p>There can be multiple posts within a topic, each post (and topic) has one user attached to it. I need to be able to call data in the User table using Post and Topics.</p>
  
  <p>Question: How to get data from a table through multiple Models. (Topic -> Post -> User)</p>
  
  <p><strong>The Models</strong></p>
  
  <pre><code>class Topic &lt; ActiveRecord::Base
      belongs_to :forum
      has_many :posts, :dependent =&gt; :destroy  
      belongs_to :user
  end
  
  class Post &lt; ActiveRecord::Base  
      belongs_to :topic  
      belongs_to :user
  end
  
  class User &lt; ActiveRecord::Base
      has_many :topics
      has_many :posts, through: =&gt; :topics
  end
  </code></pre>
  
  <p>In the View/Topics/show.html, I am trying to show the user who posted. When I try to get the information, it is giving me errors of undefined methods for "name". What I want is something like:</p>
  
  <p><a href="http://i44.tinypic.com/1z4kapj.png" rel="nofollow">http://i44.tinypic.com/1z4kapj.png</a></p>
  
  <p>Example: Post 1 was created by UserName</p>
  
  <p><strong>View/Topics/Show.html.erb</strong></p>
  
  <pre><code>&lt;% @topic.posts.each do |post| %&gt;
      &lt;tr&gt;
          &lt;td class="subforum" width="80%"&gt;
              &lt;%= post.created_at %&gt;
              &lt;div class="go-right"&gt;
                  &lt;%= link_to 'Edit', edit_post_path(@post) %&gt; 
              &lt;/div&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
          &lt;td class="row1"&gt;
              &lt;%= post.users.name %&gt;
              &lt;%= post.user_id %&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="row1"&gt;
              &lt;%= post.content %&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;% end %&gt;
  </code></pre>