Q:

loop iterates over all items, for each item

I'm running into an issue where I rander a partial using AJAX and for some reason the line_items renders to the amount of products in it. So if there's 2 products, it loops through all line_items 2 times, causing it to be 4 elements listed instead of 2. So  the loop keeps going by the amount of products in the line_items.
In my create.js.erb I have:
$('#shopping-cart').html("<%= escape_javascript render(@cart) %>");

and my _cart:
<div id="shopping-cart">
  <div id="shopping-cart-header">
  </div>

  <div id="shopping-cart-items">
    <table>
      <%= render cart.line_items %>
      <tr class="total-line">
        <td colspan="2">Total</td>
        <td class="total-cell"><%= number_to_currency(cart.total_price, unit: '') %></td>
      </tr>
    </table>
    <%= button_to 'Empty cart', cart, method: :delete %>
  </div>

  <div id="shopping-cart-footer">
    <%= link_to 'New order', new_order_path, class: 'btn btn-main btn-lg', data: { no_turbolink: true } %>
  </div>
</div>

and the issues is with render cart.line_items I guess:
<% @cart.line_items.each do |item| %>
  <% if line_item == @current_item %>
    <tr id="current-item">
  <% else %>
    <tr>
  <% end %>
    <td><%= item.quantity %> &times;</td>
    <td>
      <p><%= item.product.name %></p>
      <% if item.line_item_attributes.exists? %>
        <% item.line_item_attributes.each do |attribute| %>
          <i><%= attribute.product_attribute.name %></i>
        <% end %>
      <% end %>
    </td>
    <td class="item-price"><%= number_to_currency(item.total_price, unit: '') %></td>
  </tr>
<% end %>

if I've now added 2 products they're shown twice like this:
1 × red shirt 490.00
1 × blue shirt 89.00
1 × red shirt 490.00
1 × blue shirt 89.00
Total   579.00

Any one know what's going on? My logs are:
Started POST "/line_items" for ::1 at 2016-01-25 08:53:41 +0100
Processing by LineItemsController#create as JS
  Parameters: {"utf8"=>"✓", "line_item"=>{"product_id"=>"5", "instruction"=>""}, "commit"=>"Legg til"}
  Cart Load (0.4ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT $2  [["id", 92], ["LIMIT", 1]]
  Product Load (0.3ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
   (0.3ms)  SELECT COUNT(*) FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."product_id" = $2  [["cart_id", 92], ["product_id", 5]]
  LineItem Load (0.4ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."product_id" = $2  [["cart_id", 92], ["product_id", 5]]
  LineItemAttribute Load (0.4ms)  SELECT "line_item_attributes".* FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1  [["line_item_id", 132]]
  LineItem Load (0.4ms)  SELECT  "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."id" = $2 LIMIT $3  [["cart_id", 92], ["id", 132], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Product Load (0.4ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
  SQL (11.6ms)  UPDATE "line_items" SET "quantity" = $1, "updated_at" = $2 WHERE "line_items"."id" = $3  [["quantity", 2], ["updated_at", 2016-01-25 07:53:41 UTC], ["id", 132]]
   (0.8ms)  COMMIT
  LineItem Exists (1.0ms)  SELECT  1 AS one FROM "line_items" WHERE "line_items"."cart_id" = $1 LIMIT $2  [["cart_id", 92], ["LIMIT", 1]]
  LineItem Load (0.7ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1  [["cart_id", 92]]
  Product Load (0.5ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  LineItemAttribute Exists (0.4ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 131], ["LIMIT", 1]]
  LineItemAttribute Exists (1.0ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 131], ["LIMIT", 1]]
  Product Load (0.4ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
  LineItemAttribute Exists (0.4ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 132], ["LIMIT", 1]]
  LineItemAttribute Exists (0.3ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 132], ["LIMIT", 1]]
  LineItemAttribute Exists (0.4ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 131], ["LIMIT", 1]]
  LineItemAttribute Exists (0.3ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 131], ["LIMIT", 1]]
  LineItemAttribute Exists (0.3ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 132], ["LIMIT", 1]]
  LineItemAttribute Exists (0.3ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 132], ["LIMIT", 1]]
  Rendered line_items/_line_item.html.erb (22.9ms)
  LineItemAttribute Exists (0.5ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 131], ["LIMIT", 1]]
  LineItemAttribute Exists (0.4ms)  SELECT  1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2  [["line_item_id", 132], ["LIMIT", 1]]
  Rendered carts/_cart.html.erb (74.4ms)
  Rendered line_items/create.js.erb (78.1ms)

It seems that _line_items are rendered, and then _cart (which again contains line_items) are rendered - maybe thats an issue?

A:

You're correct. When you call render cart.line_items, you're rendering a collection. To quote the Rails documentation,

When you pass a collection to a partial via the :collection option, the partial will be inserted once for each member in the collection:

<h1>Products</h1>
<%= render partial: "product", collection: @products %>

(this is the same as your render cart.line_items)
And then in the partial you will have access to a product (or line_item in your case) local variable, like this:
<p>Product Name: <%= product.name %></p>

So: Rewrite your partial view to represent a single line_item with a corresponding line_item variable instead of a collection.

