title: Ruby on Rails: Insert values via form into array field in db
body: <p>How would you insert values from a form into an array in the database? I'm using simple form, rails 4.1, postgresql db.</p>
  
  <p>Here is my form, all fields update correctly except for the impact and likelihood fields (both arrays). They are the focus of this question:</p>
  
  <pre><code>&lt;%= simple_form_for(@risk) do |f| %&gt;
    &lt;%= render 'shared/error_messages', object: f.object %&gt;
    &lt;div class="form-group"&gt;
      &lt;%= f.input :title, required: false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= f.input :description, required: false, :error =&gt; false,  as: :text, input_html: { class: 'form-control description' } %&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= f.input :area, :collection =&gt; ['Operations', 'IT', 'Finance'], required: false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= f.input :owner, :collection =&gt; User.all, required: false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= f.input :action, required: false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
    &lt;/div&gt;
  
    &lt;div class="form-inline"&gt;
      &lt;span class="date-of-action-input"&gt;
      &lt;%= f.input :date_of_action, :as =&gt; :date, :start_year =&gt; Date.today.year - 10, :end_year =&gt; 2030,
          :order =&gt; [ :day, :month, :year], :required =&gt; false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
      &lt;/span&gt;
      &lt;span class="action-completed-input"&gt;
      &lt;%= f.input :action_completed, as: :boolean, required: false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
      &lt;/span&gt;
  
      &lt;span class="impact-input"&gt;
      &lt;%= f.input :impact, required: false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
      &lt;/span&gt;
      &lt;span class="likelihood-input"&gt;
      &lt;%= f.input :likelihood, required: false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
      &lt;/span&gt;
      &lt;span class="submit-risk"&gt;
      &lt;%= f.button :submit, :error =&gt; false, :error =&gt; false, input_html: { class: 'form-control' } %&gt;
      &lt;/span&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Here the associated controller (risks_controller.rb):</p>
  
  <pre><code>class RisksController &lt; ApplicationController
  before_action :signed_in_user
  before_action :correct_user, only: [:destroy, :update]
  
  
  def create
      @risk = current_user.risks.build(risk_params)
      if @risk.save
        flash[:success] = "Risk created!"
        redirect_to root_url
      else
        render 'new'
      end
  end
  
  def new
    @risk = Risk.new
  end
  
  def destroy
    @risk.destroy
    redirect_to root_url
  end
  
  def edit
    @risk = Risk.find(params[:id])
  end
  
  def update
    @risk = Risk.find(params[:id])
      @risk.assign_attributes(risk_params)
      if @risk.changed? == false
        flash[:info] = "No changes were made"
        redirect_to root_url
      elsif @risk.update_attributes(risk_params)
        flash[:success] = "The risk has been updated."
        redirect_to root_url
      else
          render 'new'
      end
  end
  
  private
  
      def risk_params
          params.require(:risk).permit(:description, :title, :area, :owner, :action, :date_of_action, :action_completed, :impact, :likelihood)
      end
  
    def correct_user
      if current_user.admin?
        @risk = Risk.find_by(id: params[:id])
        redirect_to root_url if @risk.nil?
      else
        @risk = current_user.risks.find_by(id: params[:id])
        redirect_to root_url if @risk.nil?
      end
    end
  
  end
  </code></pre>
  
  <p>My db schema file:</p>
  
  <pre><code>ActiveRecord::Schema.define(version: 20140617165640) do
  
    create_table "risks", force: true do |t|
      t.integer  "user_id"
      t.string   "description"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.string   "title"
      t.string   "area"
      t.string   "owner"
      t.string   "action"
      t.date     "date_of_action"
      t.boolean  "action_completed", default: false
      t.integer  "impact",           default: [],    array: true
      t.integer  "likelihood",       default: [],    array: true
    end
  
    add_index "risks", ["user_id", "created_at"], name: "index_risks_on_user_id_and_created_at", using: :btree
  
    create_table "users", force: true do |t|
      t.string   "name"
      t.string   "email"
      t.boolean  "admin",           default: false
      t.datetime "created_at"
      t.datetime "updated_at"
      t.string   "password_digest"
      t.string   "remember_token"
    end
  
    add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
    add_index "users", ["remember_token"], name: "index_users_on_remember_token", using: :btree
  
  end
  </code></pre>
  
  <p>I imagined the code would look something like this, but alas, my first rails project and I need a bit of guidance. In risks_controller.rb:</p>
  
  <pre><code>def create
    @risk = current_user.risks.build(risk_params)
    @impact = risk_params[:impact]
    @userid = current_user.id
    @impacttoupdate = Risk.select(:impact).where(id: risk_params[:id])
    @impacttoupdate.insert(@userid, @impact)
      if @risk.save
        flash[:success] = "Risk created!"
        redirect_to root_url
      else
        render 'new'
      end
  end
  </code></pre>
  
  <p>I want to insert the value according to the user id. So if the user id is 3, the value from the form they are submitting should go into the 3rd slot in the array.</p>