ActiveRecord + ActiveAdmin, save a empty string instead of null

Cornelius

When I use the Active Admin form, the empty string values are saved as "" (empty) instead of NULL value.

Is there a parameter I should set in the initializer to save every empty values as NULL in MySQL?

form do |f|
    input :label
    input :description, as: :text
    input :country
    input :city
  end
  actions
end

This is my migration schema:

create_table "projects" do |t|
  t.string "label", limit: "40"
  t.string "country", limit: "2"
  t.string "city", limit: "200"
  t.string "description", limit: 600
end

And this is my Gemfile.

gem 'rails', '~> 5.1.0.rc1'
gem 'mysql2', '>= 0.3.18', '< 0.5'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'active_model_serializers', '~> 0.10.5', require: true
gem 'kaminari'
gem 'inherited_resources', '~> 1.7'
gem 'activeadmin', '~> 1.0.0.pre5'
gem 'sidekiq', '~> 4.2.10'
gem "paperclip", "~> 5.1.0"
gem 'aws-sdk', '~> 2.3.0'
Md. Farhan Memon

That seems to be the default behaviour of forms. I came accross this gem nilify_blanks which will solve your issue. Hope it helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Return Empty String Instead of Null

Rails collection_select: save null instead of empty string when 'blank' option selected

Want to send empty json string "{}" instead of "null"

Golang Insert NULL into sql instead of empty string

simpleframework, deserializing an empty element to an empty string instead of null

Why is the default value of the string type null instead of an empty string?

SQL split string and get NULL values instead of empty string

Class member is json-serialized as empty string instead of null

Marshalling empty map[string]interface{} results in "null" instead of nil

EF/ASP Core handle string property as NULL instead of empty

How to get null value instead of empty string with Glass Mapper?

Convert.ToString returns string.empty instead of null

laravel: how to set my resource to give empty string instead of null

Why String.prototype.match() returns null instead of empty array?

Rails active model serializer, how to return empty string instead of null

How to save null strings as string.empty when saving changes?

How to save session in ActiveRecord instead of cookie

Can my empty TextInput value be sent to the DataProvider as empty string instead of converted to null?

jq: pick null instead of empty

Return empty list instead of null

Return empty array instead of null

null value or empty string

Check if a string is not NULL or EMPTY

Empty string vs NULL

Variant/String instead of Variant/Empty

Save an empty string to the database in Rails

nextLine() method save empty string

Can I save memory by writing SQL instead of ActiveRecord?

How do i return data where that contain empty string instead of NULL in laravel