How to save ids to users columns
<p>So I've built a system of products and a shopping cart in my rails app. The goal I have is to add ids of the saved products from a cart to the user model. So in my cart view page there is a list of all added products in a cart and I want to add a save button which will save those products by their ids to the columns in users table. As an example, if current_user ads three products in the cart with ids 1,2,3 and clicks on "Save" button in a cart, I want to be able to save those three ids by integers to the three columns: product_one, product_two, product_three of the current_user.</p>

<p>So far these are my models:</p>

<pre><code>class Item &lt; ActiveRecord::Base
    has_one :cart
end

class User &lt; ActiveRecord::Base

  has_one :cart
  has_many :items, through: :cart 
end

class Cart &lt; ActiveRecord::Base

  belongs_to :user
  belongs_to :item

  validates_uniqueness_of :user, scope: :item
end
</code></pre>

<p>My controllers:</p>

<pre><code>class ItemsController &lt; ApplicationController
  before_action :set_item, only: [:show, :edit, :update, :destroy]

  respond_to :html, :json, :js

  def index
    @items = Item.where(availability: true)
  end 

  def show
  end 

  def new 
    @item = Item.new
  end 

  def edit
  end 

  def create
    @item = Item.new(item_params)
    @item.save
    respond_with(@item)
  end 

  def update
    @item.update(item_params)
    flash[:notice] = 'Item was successfully updated.'
    respond_with(@item)
  end 

  def destroy
    @item.destroy
    redirect_to items_url, notice: 'Item was successfully destroyed.'
  end 

  private
    def set_item
      @item = Item.find(params[:id])
    end 

    def item_params
      params.require(:item).permit(:name, :description, :availability) 
    end 
end
</code></pre>

<p>my cart controller:</p>

<pre><code>class CartController &lt; ApplicationController

  before_action :authenticate_user!, except: [:index]


  def add
    id = params[:id]
    if session[:cart] then
      cart = session[:cart]
    else
      session[:cart] = {}
      cart = session[:cart]
    end
    if cart[id] then
      cart[id] = cart[id] + 1
    else
      cart[id] = 1
    end
  redirect_to :action =&gt; :index
  end


  def clearCart
    session[:cart] = nil
    redirect_to :action =&gt; :index
  end






  def index
    if session[:cart] then
      @cart = session[:cart]
    else
      @cart = {}
    end

  end
end
</code></pre>

<p>And I'm using Devise for authentication..</p>