title: Append a string to an array through strong parameters using << or Find_By_Sql in Rails
body: <p>I am trying to append a string value to two array columns ("good_product" &amp; "bad_product") of a Postgresql model ("product.rb") using strong parameters with user input from form_for text fields. I have found two ways (&lt;&lt; &amp; find_by_sql) to achieve this goal but can't get either to work. </p>
  
  <p>For example, "Product 3" submitted through form_for input should be added to {"Product 1", "Product 2", _______ } (if other products exist in the array) OR an empty array (if no product added yet).</p>
  
  <p>Model: product.rb</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
    serialize :good_product, Array
    serialize :bad_product, Array
  end
  </code></pre>
  
  <p>Controller: products.controller.rb  </p>
  
  <pre><code>def new
    @product = Product.new
  end
  
  def create
    @product = Product.new(product_params)
    @product.good_product ||= []
    @product.bad_product ||= []
    @product.good_product &lt;&lt; :good_product
    @product.bad_product &lt;&lt; :bad_product
    redirect_to '/products'
  end
  
  private
    def product_params
      params.require(:product).permit(:good_product =&gt; [], :bad_product =&gt; [])
    end
  end
  </code></pre>
  
  <p>View: new.html.erb</p>
  
  <pre><code>&lt;%= form_for @product, url: products_path do |f| %&gt;
    &lt;%= f.label :good_product, "Good Product" %&gt;
    &lt;%= f.text_field :good_product %&gt;
  
    &lt;%= f.label :bad_product, "Bad Product" %&gt;
    &lt;%= f.text_field :bad_product %&gt;
  
    &lt;%= f.submit 'Submit' %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Schema: schema.rb</p>
  
  <pre><code>create_table "products", force: :cascade do |t|
    t.string   "good_product", default: [], array: true
    t.string   "bad_product", default: [], array: true
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
  
  add_index "products", ["user_id"], name: "index_products_on_user_id", using: :btree
  </code></pre>
  
  <p>Alternatively, instead of "&lt;&lt;", I tried this in the Controller:</p>
  
  <pre><code>def create
    @product = Product.new(product_params)
    Product.find_by_sql(["UPDATE products SET good_product = array_append(good_product, '?') WHERE user_id = ?", @product.good_product, current_user]);
    Product.find_by_sql(["UPDATE products SET bad_product = array_append(bad_product, '?') WHERE user_id = ?", @product.bad_product, current_user]);
    redirect_to '/products'
  end
  </code></pre>
  
  <p>Thank you all for your help!</p>