postgresql - Rails string dont allow empty string value in DB on create -


i have model looks in schema.rb

create_table "runs", force: true |t|     ...     t.string   "label", default: "default run title", null: false     ...     t.datetime "timestamp" end 

however, when create new run leaving label field blank in form, stores run like:

=> #<run... label: "", .....> 

i want force default value set default: "default run title if string passed empty string.

what missing?

i suppose can use validator method or before_save or something, i'd rather have model govern behavior since within realm of default => supposed thought...

putting junk in database schema really, annoying. if later need change phrasing? need run migration. if want phrasing change based on user's language? need write hack work around it.

what's better put in model:

before_validation :assign_default_label 

then later have method defaults it:

def assign_default_label   return if (self.label?)    self.label = "default run title" end 

any time need change phrasing can re-deploy without having alter schema.

the label? method in model return true if there's title assigned contains other spaces. means blank titles replaced whatever phrasing want. these methods automatically generated activerecord.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -