title: Rails group_id to Insert statement
body: <p>I am trying to add a group(int) to my workorder_controller. I want to store all the workorder data in the same table but filter results based on group not user_id using devise. The idea is, users can see all the user entries for only their group. If there is better way of doing this please let me know. </p>
  
  <p>I have thought about using: 
  <a href="https://stackoverflow.com/questions/32698367/why-am-i-getting-nomethoderror-for-this-devise-group-call">Why am I getting NoMethodError for this devise_group call?</a></p>
  
  <p>It looked like it wasn't working correctly</p>
  
  <p>I also tried to follow this will no luck either:
  <a href="https://stackoverflow.com/questions/29904610/adding-group-id-to-notes">Adding group_id to notes</a></p>
  
  <p><strong>workorder_controller.rb</strong></p>
  
  <pre><code>class WorkordersController &lt; ApplicationController
    before_action :authenticate_user!
    load_and_authorize_resource
    before_action :set_workorder, only: [:show, :edit, :update, :destroy]
  
    # GET /workorders
    # GET /workorders.json
    def index
      @workorders = Workorder.where(group: current_user.group)
    end
  
    # GET /workorders/1
    # GET /workorders/1.json
    def show
    end
  
    # GET /workorders/new
    def new
      @workorder = current_user.workorders.build
    end
  
    # GET /workorders/1/edit
    def edit
    end
  
    # POST /workorders
    # POST /workorders.json
    def create
      @workorder = current_user.workorders.build(workorder_params)
  
      respond_to do |format|
        if @workorder.save
          format.html { redirect_to @workorder, notice: 'Workorder was successfully created.' }
          format.json { render :show, status: :created, location: @workorder }
        else
          format.html { render :new }
          format.json { render json: @workorder.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /workorders/1
    # PATCH/PUT /workorders/1.json
    def update
      respond_to do |format|
        if @workorder.update(workorder_params)
          format.html { redirect_to @workorder, notice: 'Workorder was successfully updated.' }
          format.json { render :show, status: :ok, location: @workorder }
        else
          format.html { render :edit }
          format.json { render json: @workorder.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /workorders/1
    # DELETE /workorders/1.json
    def destroy
      @workorder.destroy
      respond_to do |format|
        format.html { redirect_to workorders_url, notice: 'Workorder was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_workorder
        @workorder = Workorder.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def workorder_params
        params.require(:workorder).permit(:contractor_id, :description, :estimatedtime, :startdate, :completiondate, :budgetoverhead, :group)
      end
  end
  </code></pre>
  
  <p><strong>application_controller.rb</strong></p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    protect_from_forgery with: :exception
      before_action :configure_permitted_parameters, if: :devise_controller?
  
    protected
  
    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up)  { |u| u.permit(  :email,:password,:group ,:password_confirmation, roles: []) }
    end
  end
  </code></pre>
  
  <p><strong>ability.rb</strong></p>
  
  <pre><code>class Ability
    include CanCan::Ability
  
    def initialize(user)
      # Define abilities for the passed in user here. For example:
      #
      user ||= User.new
    if user.has_role? :admin
      can :manage, :all
    elsif user.has_role? :client
      #workorders
      can :create, Workorder 
      can :update, Workorder 
      can :destroy, Workorder 
  
  
  
      can :read, :all #can read all items bound only to the user login not all users
    else user.has_role? :contractor
      #workorders
      can :create, Workorder 
      can :update, Workorder 
      can :destroy, Workorder 
  
  
      can :read, :all #can read all items bound only to the user login not all users
  end
      #
      # The first argument to `can` is the action you are giving the user
      # permission to do.
      # If you pass :manage it will apply to every action. Other common actions
      # here are :read, :create, :update and :destroy.
      #
      # The second argument is the resource the user can perform the action on.
      # If you pass :all it will apply to every resource. Otherwise pass a Ruby
      # class of the resource.
      #
      # The third argument is an optional hash of conditions to further filter the
      # objects.
      # For example, here the user can only update published articles.
      #
      #   can :update, Article, :published =&gt; true
      #
      # See the wiki for details:
      # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
    end
  end
  </code></pre>
  
  <p>Output from console:</p>
  
  <pre><code>Started POST "/workorders" for 127.0.0.1 at 2017-10-19 18:21:18 -0500
  Processing by WorkordersController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"XXXXXtoken hereXXXXX", "workorder"=&gt;{"contractor_id"=&gt;"1", "description"=&gt;"qadsddas", "estimatedtime"=&gt;"111", "startdate(1i)"=&gt;"2017", "startdate(2i)"=&gt;"10", "startdate(3i)"=&gt;"19", "completiondate(1i)"=&gt;"2017", "completiondate(2i)"=&gt;"10", "completiondate(3i)"=&gt;"19", "budgetoverhead"=&gt;"1111"}, "commit"=&gt;"Create Workorder"}
    User Load (0.7ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
     (3.4ms)  BEGIN
    Contractor Load (0.8ms)  SELECT  `contractors`.* FROM `contractors` WHERE `contractors`.`id` = 1 LIMIT 1
     (0.3ms)  ROLLBACK
    Rendering workorders/new.html.erb within layouts/application
    Contractor Load (0.7ms)  SELECT `contractors`.* FROM `contractors` WHERE `contractors`.`group` = 1
    Rendered workorders/_form.html.haml (27.3ms)
    Rendered workorders/new.html.erb within layouts/application (32.2ms)
  Completed 200 OK in 136ms (Views: 99.4ms | ActiveRecord: 5.9ms)
  </code></pre>