0

I'm using rails 4.2 with ruby 2.2. I'm trying to save some data inside a column in a hash format.

I'm trying to save license details in the license column which is of text datatype, and in the respective model i've given:

serialize :license, JSON

I have this in the controller:

params.require(:company_detail).permit(:company_name, :trading_name, :contact_name, :acn, :address, :suburb, :state, :post_code, :phone_number, :fax_number, :nrma_approved, :start_date, :release_date , :website_url ,:email, license: {date1: :date1, license1: :license1, date2: :date2, license2: :license2, date3: :date3, license3: :license3, date4: :date4, license4: :license4})

and in form

= form_for @company_detail, remote: true, html: {class: 'form-horizontal form-label-left', data: {"parsley-validate" => ""}} do |f|
      = f.fields_for :license do |l|
        .row
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Date 1
              .col-md-8.col-xs-12
                = l.text_field :date1, value: @company_detail.license.present? ? @company_detail.license["date1"] : '', class: 'date-picker form-control has-feedback-left datePicker'
          .col-lg-6
            .form-group
              %label.control-label.col-md-4.col-xs-12 Licence Number 1
              .col-md-8.col-xs-12
                = l.text_field :license1, value: @company_detail.license.present? ? @company_detail.license["license1"] : '', class: 'form-control'

I tried entering a few fields in the form and submit, but even though the params are going in correctly, the value saved is null. This is the log:

Parameters: {"utf8"=>"✓", "company_detail"=>{"license"=>{"date1"=>"121212", "license1"=>"12313", "date2"=>"122131123", "license2"=>"123123", "date3"=>"", "license3"=>"", "date4"=>"", "license4"=>""}}, "commit"=>"Save", "id"=>"1"}
CompanyDetail Load (0.3ms)  SELECT  "company_details".* FROM "company_details" WHERE "company_details"."id" = $1 LIMIT 1  [["id", 1]]
 User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
 (1.0ms)  BEGIN
SQL (0.4ms)  UPDATE "company_details" SET "license" = $1, "updated_at" = $2 WHERE "company_details"."id" = $3  [["license", "{\"date1\":null,\"license1\":null,\"date2\":null,\"license2\":null,\"date3\":null,\"license3\":null,\"date4\":null,\"license4\":null}"], ["updated_at", "2016-05-01 12:15:44.918547"], ["id", 1]]
Alfie
  • 2,558
  • 1
  • 12
  • 23
Anbazhagan P
  • 115
  • 9

2 Answers2

0

Try this for your strong parameters for licence:

license: [:date1, :license1, :date2, :license2, :date3, :license3, :date4, :license4]

tirdadc
  • 4,255
  • 3
  • 35
  • 43
  • Hi @tirdadc Your answer helped, can i do a where query in serialized column – Anbazhagan P May 01 '16 at 12:38
  • If you want to do queries on the data, you should use the JSONB datatype for your column as @Alfie suggested. Serialized data is mostly meant for reading/writing. – tirdadc May 01 '16 at 12:40
  • See this SO answer for more info on using JSONB (you'll need Rails 4.2+ too): http://stackoverflow.com/questions/27462929/how-to-make-rails-4-2-work-with-postgres-jsonb – tirdadc May 01 '16 at 12:42
0

If you are using postgres as your db, then you can use json as the datatype for your column.

See here - ActiveRecord JSON

If you are looking into querying your json fields, you should use the jsonb field. Reference

But please note jsonb datatype was only introduced in postgresql v9.4

Alfie
  • 2,558
  • 1
  • 12
  • 23