title: Rails how to attach object creation to a user session for user_id
body: <p>I'm new to rails. I'm working on a simple app for creating and sharing collection with friends. i have Users, who have items. These items belong to categories, which it turn have subcategories. Users can place comments on items</p>
  
  <p>User has_many items, Item belongs_to user</p>
  
  <p>Category has_many items, Items belong to category</p>
  
  <p>Category has_many subcategories, Subcategories belong_to category</p>
  
  <p>item has_many comments, comments belong to item.</p>
  
  <p>I've managed to create a separate from for creating comments, with options to select categories and subcategories. This automatically updates the category_id and subcategory_id in the items table:</p>
  
  <p>views/item/new.html.erb</p>
  
  <pre><code>div class="field"&gt;
    &lt;%= f.label :item_name %&gt;&lt;br /&gt;
    &lt;%= f.text_field :name %&gt;
   &lt;/div&gt;
  
   &lt;div class="field"&gt;
      &lt;%= f.label :category %&gt;&lt;br /&gt;
      &lt;%= f.collection_select :category_id, @categories, :id, :category_type, prompt: true %&gt;
   &lt;/div&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :subcategory %&gt;&lt;br /&gt;
      &lt;%= f.collection_select :subcategory_id, @subcategories, :id, :sub_type, prompt: true %&gt;
   &lt;/div&gt;
  
   &lt;div class="field"&gt;
    &lt;%= f.label :description %&gt;&lt;br /&gt;
    &lt;%= f.text_area :description, rows: 7%&gt;
   &lt;/div&gt;
  
   &lt;div class="actions"&gt; 
      &lt;%= f.submit %&gt;
   &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>controllers/items_contoller.rb</p>
  
  <p>class ItemsController &lt; ApplicationController</p>
  
  <pre><code>   def index
      @items = Item.all
  
    end
  
    def new
      @item = Item.new
      @categories = Category.all
      @subcategories = Subcategory.all 
  
    end
  
    def edit
      @item = Item.find(params[:id])
      @categories = Category.find.all
      @subcategories = Subcategory.all 
  
    end
  
    def show
      @item = Item.find(params[:id])
  
       render 'show'
      end
  
    def create
      @item = Item.new(params[:item])
  
      if @item.save
        flash[:success] = "Added new item #{@item}"
        redirect_to @item
      else
        render 'new'
      end
    end
  
    def update
      @item = Item.find(params[:id])
  
      if @item.update_attributes(params[:item])
       flash[:success] = "Successfully updated item!"
      redirect_to @item 
      else
       render 'edit'
      end  
    end
  
    def destroy
      @item = Item.find(params[:id])
      @item.destroy
      flash[:success] = "Successfully deleted category!"
      redirect_to categories_url, notice: "deleted"
    end
  
  end
  </code></pre>
  
  <p>But I have a problem with associating the particular item to a user when creationg or editing. I already have a working session management for users. Users can login and logout. i would like to be able to also update the user_id column in the items table with the corresponding user_id of the user in the current session but I have no idea how to go about it. If it helps, below is how I have handle session management</p>
  
  <p>sessions_controller.rb</p>
  
  <p>class SessionsController &lt; ApplicationController</p>
  
  <pre><code> def new
    end
  
    def create
      user = User.find_by_email(params[:session][:email].downcase)
      if user &amp;&amp; user.authenticate(params[:session][:password])
        sign_in user
        redirect_to user
      else
       render 'new', notice:"Invalid email/password combination" 
      end
    end
  
    def destroy
      sign_out
      redirect_to root_url
    end
  end
  </code></pre>
  
  <p>sessions_helper.rb</p>
  
  <pre><code>module SessionsHelper
  
    def sign_in(user)
      cookies.permanent[:remember_token] = user.remember_token
      self.current_user = user
    end
  
    def signed_in?
      !current_user.nil?
    end
  
    def current_user=(user)
      @current_user = user
    end
  
    def current_user
      @current_user ||= User.find_by_remember_token(cookies[:remember_token])
    end
  
    def current_user?(user)
      user == current_user
    end 
  
    def sign_out
      self.current_user = nil
      cookies.delete(:remember_token)
    end
  end
  </code></pre>
  
  <p>many thanks in advance for your anticipated respinse</p>