title: Rails: How to save a cart to a user session?
body: <p>In my application I have designed a cart while following a similar format to the layout in the book <em>Agile Web Development With Rails 4</em> and I have a slight problem. So a user can add items to a cart, view their cart, and see price of each item and also the total. The issue I ran into is when a user puts items in a  cart and then signs out, the cart keeps the items in the cart even when a different user signs in. I believe the proper way would be that each user would have their own individual cart. </p>
  
  <p>Here is my user model and controller </p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_one :cart 
    # :confirmable, :lockable, :timeoutable and :omniauthable
    # :confirmable, :lockable, :timeoutable and :omniauthable
    has_secure_password
    validates :email, presence: true
  end
  
  class UsersController &lt; ApplicationController
    def new
      @user = User.new
    end
  
    def show
      @user = User.find(params[:id])
    end
  
    def create
      @user = User.new(user_params)
        if @user.save
          session[:user_id] = @user.id
          redirect_to @user
        else
          render 'new'
        end
    end
  
    def edit
      @user = User.find(params[:id])
    end
  
    def update
      @user = User.find(params[:id])
        if @user.update_attributes(user_params)
          redirect_to @user
        end
    end
  
    private
    def user_params
      params.require(:user).permit(:first_name, :admin, :last_name, :email, :password, :password_confirmation, :phone_number, :address_one, :address_two, :city, :country, :state, :zip)
      end
  end
  </code></pre>
  
  <p>My cart model and controller </p>
  
  <pre><code>class Cart &lt; ActiveRecord::Base
    has_many :order_items
    belongs_to :user
    has_many :line_items, dependent: :destroy
  
    def add_part(part_id)
      current_part = line_items.find_by(part_id: part_id)
      if current_part
        current_part.quantity += 1
      else
        current_part = line_items.build(part_id: part_id)
      end
      current_part
    end
  
    def total_price
      line_items.to_a.sum { |item| item.total_price}
    end 
  end
  
  class CartsController &lt; ApplicationController
    before_action :set_cart, only: [:show, :edit, :update, :destroy]
    rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
  
    def show
      @cart = Cart.find(params[:id])
    end
  
    def edit
      @cart = Cart.new(cart_params)
    end
  
    def update
      @cart = Cart.find(params[:id])
        if @cart.update_attributes(cart_params)
          redirect_to @cart
        end
    end
  
    def destroy
      @cart.destroy if @cart.id == session[:cart_id]
      session[:cart_id] = nil
      respond_to do |format|
        format.html { redirect_to root_path }
        format.json { head :no_content }
    end
  end
  
    private
    def cart_params
      params.require(:cart).permit(:user_id)
      end
  
     def invalid_cart
       logger.error "Attempt to access invalid cart #{params[:id]}"
       redirect_to root_path, notice: "Invalid cart"
     end
  end
  </code></pre>
  
  <p>and my Line Items controller and current module (line items associates a part to a cart in my layout) </p>
  
  <pre><code>class LineItemsController &lt; ApplicationController
    include CurrentCart
    before_action :set_cart, only: [:create]
    before_action :set_line_item, only: [:show, :edit, :update, :destroy]
  
  
    def create
      part = Part.find(params[:part_id])
      @line_item = @cart.add_part(part.id)
  
      respond_to do |format|
         if @line_item.save
           format.html { redirect_to @line_item.cart }
          format.json { render action: 'show', status: :created,
            location: @line_item }
          else
          format.html { render action: 'new' }
          format.json { render json: @line_item.errors,
          status: :unprocessable_entity }
        end
      end
    end
  end
  
  module CurrentCart
    extend ActiveSupport::Concern
  
    private
  
        def set_cart
          @cart = Cart.find(session[:cart_id])
        rescue ActiveRecord::RecordNotFound
          @cart = Cart.create
          session[:cart_id] = @cart.id
        end
      end 
  </code></pre>
  
  <p>Any advice on how I can get one cart to be associate with one user would be a big help :) if anymore information is needed just ask. Thanks again! </p>