title: NoMethodError undefined method `name' for nil:NilClass
body: <p>I have two models post and category. I'm trying to show the category name for each post in both my index and show view of post. I'm using table join. But the problem is though in my show view the category is showing properly, but its giving a NoMethodError: undefined method `name' for nil:NilClass in the index view. I can't figure out why it's showing in my show view but not in the index view.</p>
  
  <p>index.html.erb</p>
  
  <pre><code>&lt;% @posts.each do |post| %&gt;
    &lt;h2&gt;&lt;%= link_to post.title, post %&gt;&lt;/h2&gt;
    &lt;p&gt;বিভাগঃ &lt;%= post.category.name %&gt;&lt;/p&gt;
    &lt;p&gt;&lt;%= post.body %&gt;&lt;/p&gt;
    &lt;%= link_to 'দেখুন', post, class: "button tiny" %&gt;&amp;nbsp;
    &lt;%= link_to 'সম্পাদনা', edit_post_path(post), class: "button tiny" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>show.html.erb</p>
  
  <pre><code>&lt;h2&gt;&lt;%= link_to @post.title, @post %&gt;&lt;/h2&gt;
  &lt;h5&gt;বিভাগঃ &lt;%= @post.category.name %&gt;&lt;/h5&gt;
  &lt;p&gt;&lt;%= @post.body %&gt;&lt;/p&gt;
  </code></pre>
  
  <p>post.rb</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    validates_presence_of :title, :body, :category
    has_many :comments
    belongs_to :category
  end
  </code></pre>
  
  <p>category.rb</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
    has_many :posts
  end
  </code></pre>