title: NoMethodError when trying to reference a column from a different table - Ruby on Rails
body: <p>I have two tables Head and Category</p>
  
  <p>In head.rb</p>
  
  <pre><code>has_many :categories
  attr_accessible :name_subcategory, :quote, :statement
  </code></pre>
  
  <p>In category.rb</p>
  
  <pre><code>belongs_to :heads
  attr_accessible :image_url, :string, :title, :show_on_home_page, :show_on_category_page, :show_in_footer, :float_subcategory, :name_subcategory
  </code></pre>
  
  <p>In my view file</p>
  
  <pre><code>&lt;% @category.each do |category| %&gt;
  &lt;%= category.heads.name_subcategory %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>At runtime I get the following error:</p>
  
  <blockquote>
    <p>undefined method `name_subcategory' for nil:NilClass</p>
  </blockquote>
  
  <p>I'm pretty new to rails so I think this has something to do with not making the proper relationships between the tables but I'm pretty stumped as to the exact problem. I tried to research the error but it seems to be very broad and can be caused by a wide range of problems and I'm having trouble pinpointing exactly where I'm going wrong. </p>
  
  <p>I hope someone here can help!</p>
  
  <p><strong>Edit</strong>
  What I'm trying to achieve: Each head contains many categories and I'm trying to print out the head associated with a particular category (I know the naming is strange, this is a team project that I've only just joined)</p>