title: Ruby array only storing last value, not all values
body: <p>I have a Ruby app with Products and Categories. </p>
  
  <p>I would like to have a category index page that displays 1 product photo for each category.</p>
  
  <p>Right now, it shows the <strong>last</strong> product photo for all categories. (So, with 16 categories, it shows the product photo for category 16 on every category.)</p>
  
  <p>I think I need to fix how I store or call the Products array. How do I do that?</p>
  
  <p>Code --</p>
  
  <p>in categories_controller.rb</p>
  
  <pre><code>def index
    @categories = Category.all.order('name ASC')
    @products = []
  
    @categories.each do |category|
      @products = Product.where(category_id: category.id).take(1)
    end
  end
  </code></pre>
  
  <p>in categories/index.html.erb</p>
  
  <pre><code>&lt;% @categories.each do |category| %&gt;
      &lt;% @products.each do |product| %&gt;
        &lt;%= link_to image_tag(product.image), category %&gt;
      &lt;% end %&gt;
    &lt;%= link_to category.name, category %&gt;
    &lt;%= category.description %&gt;
  </code></pre>