Sujith Sujith - 1 month ago 4
Ruby Question

Hash not inserting into database

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

Answer

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 hash with dynamic keys in params?