0

I am trying to insert array into data base. I can see the array (status"=>{"group_one"=>"One", "group_two"=>"One"}) in the parameters but while inserting inot db, it is being ommitted.

My console displays the following:

Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"JgCBl8vcDAP9AUGaylk34om4mbKkxHCgM+GuAfxTL3sywaJkmvKL3pyS2C44MAkzMZ6AK+CUVv/Vmg9aUQYrZw==", 
"irb"=>{"proposalno"=>"", "otherapptype"=>"", "titleofproject"=>"",
 "date1"=>"", "date2"=>"", "date3"=>"", "fundingdetails"=>"", 
"fund"=>"No internal funds or external funds are requested", 
"datetobegindc"=>"", "rationale"=>"", "abstract"=>"", 
"noofsub"=>"0", "natureofassociation"=>"", 
"subjectselection"=>"", "confidentiality"=>"", 
"howwhereconsent"=>"", "methodproceduresubjectparti"=>"",
"childrenpermission"=>"", "infowithheld"=>"", 
"riskbenefitratio"=>"", "minimizingrisk"=>""}, 
"status"=>{"group_one"=>"One", "group_two"=>"One"}, 
"responsibility"=>{"nameoffac"=>"", "nameofinv"=>"", 
"deptoffac"=>"", "deptofinv"=>"", "addoffac"=>"", 
"addofinv"=>"", "phoneoffac"=>"", "phoneofinv"=>"", 
"emailoffac"=>"", "emailofinv"=>""}, "commit"=>"SUBMIT MY DETAILS"}
   (0.2ms)  begin transaction
  SQL (2.9ms)  INSERT INTO "irbs" ("proposalno", "titleofproject",
 "date1", "date2", "date3", "fund", "fundingdetails", "datetobegindc",
 "rationale", "abstract", "noofsub", "natureofassociation",
 "subjectselection", "confidentiality", "howwhereconsent", 
"methodproceduresubjectparti", "childrenpermission", "infowithheld",
 "riskbenefitratio", "minimizingrisk", "otherapptype", "created_at", 
"updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
 ?, ?, ?, ?, ?, ?)  [["proposalno", ""], ["titleofproject", ""], 
["date1", ""], ["date2", ""], ["date3", ""], 
["fund", "No internal funds or external funds are requested"],
 ["fundingdetails", ""], ["datetobegindc", ""], ["rationale", ""], 
["abstract", ""], ["noofsub", "0"], ["natureofassociation", ""],
 ["subjectselection", ""], ["confidentiality", ""], 
["howwhereconsent", ""], ["methodproceduresubjectparti", ""], 
["childrenpermission", ""], ["infowithheld", ""], 
["riskbenefitratio", ""], ["minimizingrisk", ""], 
["otherapptype", ""], ["created_at", 2016-10-18 19:48:35 UTC], 
["updated_at", 2016-10-18 19:48:35 UTC]]

Here is my controller:

class IrbsController < ApplicationController

  def new
    @irb = Irb.new
  end



  def create
     @irb = Irb.new(irb_params)
    if @irb.save
      #RegistrationMailer.signup_confirmation(@registration).deliver
      #log_in @user
      flash[:success] = "Your details have been registered. A confirmation email has been sent."
      redirect_to root_url
    else
      render 'new'
    end
  end

  def index
  end

  def edit
  end

  def show
  end

  private
  def irb_params
      params.require(:irb).permit(:proposalno, :apptype, :titleofproject, :acc1, :date1, :acc2, :date2, :acc3, :date3, :projtype, :fund, :fundingdetails, :datetobegindc, :statusofprininv, :typeofreview, :rationale, :abstract, :noofsub, :assowithsub, :natureofassociation, :subjectselection, :ressubcomp, :adforresparti, :confidentiality, :voluntaryparticipation, :howwhereconsent, :methodproceduresubjectparti, :childrenpermission, :infowithheld, :risk, :riskbenefitratio, :minimizingrisk, :otherapptype, status:[])
  end
end

Here is the migration:

class CreateIrbs < ActiveRecord::Migration[5.0]
  def change
    create_table :irbs do |t|
      t.string :proposalno
      t.text :status
      t.string :responsibility
      t.string :apptype
      t.string :titleofproject
      t.string :acc1
      t.string :date1
      t.string :acc2
      t.string :date2
      t.string :acc3
      t.string :date3
      t.string :projtype
      t.string :fund
      t.string :fundingdetails
      t.string :datetobegindc
      t.string :statusofprininv
      t.string :typeofreview
      t.string :rationale
      t.string :abstract
      t.string :poputype
      t.string :noofsub
      t.string :assowithsub
      t.string :natureofassociation
      t.string :subjectselection
      t.string :ressubcomp
      t.string :adforresparti
      t.string :confidentiality
      t.string :voluntaryparticipation
      t.string :howwhereconsent
      t.string :methodproceduresubjectparti
      t.string :childrenpermission
      t.string :infowithheld
      t.string :risk
      t.string :riskbenefitratio
      t.string :minimizingrisk
      t.string :otherapptype
      t.timestamps
    end
  end
end

Model code:

class Irb < ApplicationRecord
  serialize :status
end
Sujith
  • 13
  • 3
  • 1
    You need to add more detail, before anyone can help you here – C dot StrifeVII Oct 18 '16 at 20:09
  • @CdotStrifeVII I added my Controller code. Please have a look. – Sujith Oct 18 '16 at 20:21
  • Perhaps it is just me, but I don't find an array in the parameter list. How is your array named? – spickermann Oct 18 '16 at 20:22
  • According to your `irb_params` method, it looks like you are attempting to insert `status` as an array... but if you look at the SQL generated - there is no mention of `status` as a column (look at the part starting `INSERT INTO`). Have you run your migration that adds the status column? Are you sure that your migration ran correctly? (have I guessed the wrong column? if so - please update your question to accurately reflect what you are trying to do and what *exactly* is failing) – Taryn East Oct 18 '16 at 20:25
  • @spickermann ""status"=>{"group_one"=>"One", "group_two"=>"One"}" – Sujith Oct 18 '16 at 20:25
  • ^^^ this should be part of your question. – Taryn East Oct 18 '16 at 20:26
  • @TarynEast I did the migration and I defined status as a string in my table – Sujith Oct 18 '16 at 20:26
  • I don't think it worked, or we'd be seeing the `status` column in the list of columns. Hmmm you defined it as a `string` but you're trying to put an `array` into it... and yet your params don't include either, but have a `hash` you know those things are three different things right? Have you got code somewhere that converts your hash-data into a string? – Taryn East Oct 18 '16 at 20:27
  • What does your `app/models/irbs.rb` look like? – Okomikeruko Oct 18 '16 at 20:28
  • @Sujith: `"status"=>{"group_one"=>"One", "group_two"=>"One"}` is a hash, not an array. – spickermann Oct 18 '16 at 20:29
  • Does `ApplicationRecor` inherit from `ActiveRecord::Base` ? – Okomikeruko Oct 18 '16 at 20:34
  • @Okomikeruko Sorry thats a typo, it is ApplicationRecord – Sujith Oct 18 '16 at 20:41
  • And do you have a `app/models/applicationrecord.rb` file that starts with `class ApplicationRecord < ActiveRecord::Base` ? – Okomikeruko Oct 18 '16 at 20:43
  • @Okomikeruko I do have it – Sujith Oct 18 '16 at 20:45

2 Answers2

0

Perhaps you should try changing your datatype from string to text

Using Rails serialize to save hash to database

Community
  • 1
  • 1
Okomikeruko
  • 1,123
  • 10
  • 22
0

You are inserting Hash not Array over here. The difference in short - Hash are made up of the collection of keys and values.

As mentioned by okomikeruko it's better to define a column with a text datatype. Although I don't think it goes over the 255 characters here, at least in the example provided in your question.

I believe that the issue is inside your permit method. For key named :status: you define 0 keys that allowed in your Hash. That's where the problem is coming from.

All you need it's whether to specify keys ..., status: [:group_one, ...] if you know them or in the case they are dynamic to go with another solution that you can find in the links below.

https://github.com/rails/rails/issues/9454

Rails4: How to permit a hash with dynamic keys in params?

Community
  • 1
  • 1
rudkovskyi
  • 97
  • 5