title: how to check whether association exists rails
body: <p>I have two tables</p>
  
  <ol>
  <li>Post</li>
  <li>Category</li>
  </ol>
  
  <p>I have the following code in my controller:</p>
  
  <pre><code>@category = Category.find(id)
  </code></pre>
  
  <p>Suppose consider category as <code>mobile, car, bike</code></p>
  
  <p>I need to check the existence of mobile or car or bike. So that I can disable the input element.  I tried:</p>
  
  <pre><code>&lt;% @category.each do |cat| %&gt;
      &lt;% if cat.posts.exists %&gt;
          class = 'active'
      &lt;% else %&gt;
          class = 'inactive'
      &lt;% end %&gt;
      &lt;div class="#{class}"&gt;&lt;%= cat.name %&gt;&lt;/div&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>The above code always runs the else condition. </p>
  
  <p>Following are the categories:</p>
  
  <ol>
  <li>Mobile</li>
  <li>Car</li>
  <li>Bike</li>
  </ol>
  
  <p>If in my post table with column <code>category_id has the field with value 1</code> then <code>mobile should have a class active</code> and the other two should be inactive.</p>