title: Pulling records from one table (products) depending on a value in another (department)
body: <p>I have a Products table and a Departments table.
  In products model:</p>
  
  <pre><code>has_one :department
  </code></pre>
  
  <p>In departments model: </p>
  
  <pre><code>belongs_to :products
  </code></pre>
  
  <p>I also have a view in my products controller that lists all departments as so:</p>
  
  <pre><code>&lt;% @departments.each do |department| %&gt;
              &lt;div class="column_entry"&gt;
                  &lt;%= link_to image_tag(department.image_attachment), products_of_a_given_main_cat_url %&gt;
              &lt;/div&gt;
  &lt;% end %&gt; 
  </code></pre>
  
  <p>Those load up fine. When I click on one of the images I want products_of_a_given_main_category view to dynamically populate with products from the department whose image I clicked.</p>
  
  <p>In my products controller:</p>
  
  <pre><code>def products_of_a_given_main_cat
  
    @products = Product.all
    @department = Department.where(:name == :department)
  
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end
  </code></pre>
  
  <p>and in the view:</p>
  
  <pre><code>&lt;div class="the_grid_prod"&gt;
      &lt;% @products.each do |product| %&gt;
          &lt;% if product.department == @department.name %&gt;
              &lt;div class="column_entry"&gt;
              &lt;%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product.id) %&gt;
          &lt;/div&gt;
          &lt;% end %&gt;
      &lt;% end %&gt;
  &lt;/div&gt; 
  </code></pre>
  
  <p>When I click on the image for a department that I'm certain has products the page loads but there are no products. I'm certain I'm doing something very wrong but I'm not really sure what it is.</p>
  
  <p>in routes file:</p>
  
  <pre><code> get 'products/products_of_a_given_main_cat' =&gt; :products_of_a_given_main_cat, :as =&gt; :products_of_a_given_main_cat
  </code></pre>