title: RecordNotFound error in rails ecommerce app
body: <p>i am building an eCommerce app in rails which has two products Chairs and Bookcabinets, i have created two different controllers and models for chairs and bookcabinets, it all worked now i wanted the user to be able to add these products to a cart so i created a ordering system from scratch by watching a tutorial, it worked for chairs but when i add bookcabinets to shopping_cart model it gives me an error: <code>Couldn't find Chair with 'id'=</code></p>
  
  <p><strong>Server Log</strong></p>
  
  <pre><code>Started POST "/cart/items" for 127.0.0.1 at 2018-09-26 14:16:43 +0530
     (0.7ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  Processing by OrderItemsController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"wGAl7gZGXipKt7EzzZT1LuBgn2k8KnlaPagp0cQ3l6pIkr6mx8MdJAuhgkY7EEttrHjTSSpcRjqe0qZ0a2hrAA==", "bookcabinet_id"=&gt;"1", "quantity"=&gt;"40"}
    Chair Load (0.8ms)  SELECT  "chairs".* FROM "chairs" WHERE "chairs"."id" = ? LIMIT ?  [["id", nil], ["LIMIT", 1]]
  Completed 404 Not Found in 106ms (ActiveRecord: 3.8ms)
  
  
  
  ActiveRecord::RecordNotFound (Couldn't find Chair with 'id'=):
  
  app/models/shopping_cart.rb:20:in `add_item'
  app/controllers/order_items_controller.rb:8:in `create'
  </code></pre>
  
  <p><strong>My Order_items_controller</strong></p>
  
  <pre><code>class OrderItemsController &lt; ApplicationController
  
    def index
      @items = current_cart.order.items
    end
  
    def create
      current_cart.add_item(
        chair_id: params[:chair_id],
        bookcabinet_id: params[:bookcabinet_id],
        quantity: params[:quantity]
      )
  
      redirect_to cart_path
    end
  
    def destroy
      current_cart.remove_item(id: params[:id])
      redirect_to cart_path
    end
  
  end
  </code></pre>
  
  <p><strong>My Order_controller</strong></p>
  
  <pre><code>class OrdersController &lt; ApplicationController
      def new
          @order = current_cart.order
      end
  end
  </code></pre>
  
  <p>** My Order model**</p>
  
  <pre><code>class Order &lt; ApplicationRecord
      has_many :items, class_name: 'OrderItem'
  end
  </code></pre>
  
  <p><strong>My Order_item Model</strong></p>
  
  <pre><code>class OrderItem &lt; ApplicationRecord
      belongs_to :order
      belongs_to :chair
      belongs_to :bookcabinet
  end
  </code></pre>
  
  <p><strong>My Shopping_Cart Model</strong></p>
  
  <pre><code>class ShoppingCart
  
    delegate :sub_total, to: :order
  
    def initialize(token:)
      @token = token
    end
  
    def order
      @order ||= Order.find_or_create_by(token: @token) do |order|
        order.sub_total = 0
      end
    end
  
    def items_count
      order.items.sum(:quantity)
    end
  
    def add_item(chair_id:, bookcabinet_id:, quantity: 1)
      chair = Chair.find(chair_id),
      bookcabinet = Bookcabinet.find(bookcabinet_id)
  
      order_item = order.items.find_or_initialize_by(
        chair_id: chair_id,
        bookcabinet_id: bookcabinet_id
      )
  
      order_item.price = chair.price
      order_item.price = bookcabinet.price
  
      order_item.quantity = quantity
  
      ActiveRecord::Base.transaction do
        order_item.save
        update_sub_total!
      end
    end
  
  
    def remove_item(id:)
      ActiveRecord::Base.transaction do
        order.items.destroy(id)
        update_sub_total!
      end
    end
  
    private
  
    def update_sub_total!
      order.sub_total = order.items.sum('quantity*price')
      order.save
    end
  
  end
  </code></pre>
  
  <p><strong>My Create_order migration</strong></p>
  
  <pre><code>class CreateOrders &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :orders do |t|
        t.string :first_name
        t.string :last_name, null: false
        t.decimal :sub_total, precision: 15, scale: 2, null: false
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p><strong>my CreateOrderItems migration</strong></p>
  
  <pre><code>class CreateOrderItems &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :order_items do |t|
        t.belongs_to :order, null: false
        t.belongs_to :chair, null: false
        t.belongs_to :bookcabinet
        t.integer :quantity, null: false
        t.decimal :price, precision: 15, scale: 2, null: false
  
        t.timestamps
      end
  
  
    end
  end
  </code></pre>
  
  <p><strong>Routes</strong></p>
  
  <pre><code>  get '/cart', to: 'order_items#index'
    resources :order_items, path: '/cart/items'
    get '/cart/checkout', to: 'orders#new', as: :checkout
    patch '/cart/checkout', to: 'orders#create'
  </code></pre>
  
  <p><strong>Add To Cart Form</strong></p>
  
  <pre><code>&lt;div class="button my-2"&gt;
                &lt;%= form_tag order_items_path do %&gt;
          &lt;%= hidden_field_tag :bookcabinet_id, @bookcabinet.id %&gt;
          &lt;%= number_field_tag :quantity, 1 %&gt;
  
          &lt;button type="submit" class="btn cart"&gt;&lt;i class="d-inline fa fa-cart-arrow-down" aria-hidden="true"&gt;&lt;/i&gt;&lt;p class="d-inline"&gt;Add To Cart&lt;/p&gt;&lt;/button&gt;
        &lt;% end %&gt;
  </code></pre>
  
  <p><strong>Updated Server Log</strong></p>
  
  <pre><code>Started POST "/cart/items" for 127.0.0.1 at 2018-09-27 00:24:13 +0530
     (0.6ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  Processing by OrderItemsController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"gqwsV8Z/0p74Y8WPLMmfk9vmoSPCFpP+lAtVxxH3KSvWUzYYIFtbQRkAzM5yh5HS/wAzelr90LJW64joFUpGwg==", "bookcabinet_id"=&gt;"1", "quantity"=&gt;"40"}
    Bookcabinet Load (0.7ms)  SELECT  "bookcabinets".* FROM "bookcabinets" WHERE "bookcabinets"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
    Order Load (0.9ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."token" = ? LIMIT ?  [["token", "20897ec5db2636f5"], ["LIMIT", 1]]
    OrderItem Load (0.5ms)  SELECT  "order_items".* FROM "order_items" WHERE "order_items"."order_id" = ? AND "order_items"."chair_id" IS NULL AND "order_items"."bookcabinet_id" = ? LIMIT ?  [["order_id", 4], ["bookcabinet_id", 1], ["LIMIT", 1]]
     (0.2ms)  begin transaction
    Bookcabinet Load (0.3ms)  SELECT  "bookcabinets".* FROM "bookcabinets" WHERE "bookcabinets"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
     (0.4ms)  SELECT SUM(quantity*price) FROM "order_items" WHERE "order_items"."order_id" = ?  [["order_id", 4]]
     (0.2ms)  commit transaction
  Redirected to http://localhost:3000/cart
  Completed 302 Found in 557ms (ActiveRecord: 10.4ms)
  
  
  Started GET "/cart" for 127.0.0.1 at 2018-09-27 00:24:14 +0530
  Processing by OrderItemsController#index as HTML
    Order Load (0.5ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."token" = ? LIMIT ?  [["token", "20897ec5db2636f5"], ["LIMIT", 1]]
    Rendering order_items/index.html.erb within layouts/application
     (0.4ms)  SELECT SUM("order_items"."quantity") FROM "order_items" WHERE "order_items"."order_id" = ?  [["order_id", 4]]
    OrderItem Load (1.1ms)  SELECT "order_items".* FROM "order_items" WHERE "order_items"."order_id" = ?  [["order_id", 4]]
    Rendered order_items/index.html.erb within layouts/application (15.0ms)
    CACHE  (0.1ms)  SELECT SUM("order_items"."quantity") FROM "order_items" WHERE "order_items"."order_id" = ?  [["order_id", 4]]
    Rendered layouts/_header.html.erb (22.0ms)
    Rendered layouts/_footer.html.erb (1.9ms)
  Completed 200 OK in 1313ms (Views: 1304.9ms | ActiveRecord: 2.1ms)
  </code></pre>