How to force rails to store empty strings? -


i'm asking opposite of question force empty strings null; instead, want fields empty strings stored empty strings. reason want (even in contradiction to people say) want have partial uniqueness constraint on table works multiple database types (postgres, mysql, etc), described in question here.

the psuedocode schema basically:

person {   first_name : string, presence: true   middle_name : string, presence: true   last_name : string, presence: true   birth_date : string, presence: true   city_of_birth: string, presence: true   active: tinyint } 

the constraint person must unique if active; inactive people can not unique (ie, can have multiple john smiths not active, 1 active john smith).

further complication: according project specification, first_name , last_name required given user, other fields can blank.

our current solution applying partial uniqueness constraint use fact null != null, , set active tinyint null if not active , set 1 if active. thus, can use rails code in migration:

add_index :persons, [first_name, middle_name, last_name, birth_date,    city_of_birth, active], unique:true, name: "unique_person_constraint" 

in order constraint work, however, none of other fields can null. if are, 2 john smiths no other filled in fields , active = 1 still 'unique' because middle_name fields value null different each other (because null != null, regardless of column type).

however, when do

options = { first_name: "john",   middle_name: "",    last_name: "smith",   birth_date: "",   city_of_birth: "", } person = person.new(options) success = person.valid? 

success false because

middle name can't blank city of birth can't blank birth date can't blank 

so need way ensure have @ least empty strings other fields enforce partial uniqueness constraint. how can this? if rid of presence:true in model definition, appears null fields allowed, bad.

this rails 3.2.13, , can give other gems , gem versions if necessary.

along presence true, can add allow_blank true allow save empty strings.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -