What's the easiest method to setup a gender area in Rails 3? I am using PostgreSQL.
I presently get it like a string but am wondering if it might be simpler (better?) to put it together being an integer. If that's the case, wouldso would this be achieved? I wish to create a drop lower for this including three values: "Male", "Female", "None of the business".
Sorry for that fundamental question but I am curious regarding simple, guidelines.
This is exactly what domain names are for:
DROP SCHEMA tmp CASCADE; CREATE SCHEMA tmp; SET search_path=tmp; CREATE DOMAIN gender CHAR(1) CHECK (value IN ( 'F' , 'M' ) ) ; CREATE TABLE persons ( pname VARCHAR , gend tmp.gender ); INSERT INTO persons( pname, gend) VALUES ('Alice', 'F') ,('Bob', 'M') ; INSERT INTO persons( pname) VALUES ('HAL') ; INSERT INTO persons( pname, gend) VALUES ('Maurice', 'Q') ; SELECT * FROM persons;
DROP SCHEMA CREATE SCHEMA SET CREATE DOMAIN CREATE TABLE INSERT 0 2 INSERT 0 1 ERROR: value for domain gender violates check constraint "gender_check" pname | gend -------+------ Alice | F Bob | M HAL | (3 rows)
An enum appears best: http://www.postgresql.org/docs/current/static/datatype-enum.html
What about utilizing a simple
is_female, should you prefer.
In either case, nil (null) shouldn't evaluate to true or false automatically.
To be sure with Mike Johnson. ENUM's are ideal for this. ENUM's are awesome for smallish teams of values that rarely change. Gender is an ideal illustration of this. Usually you simply need 5: Male,Female,Intersex,Transgender,Unknown. Tho many opt for 3: Male,Female,Other. 2 however (Male,Female) is mean to individuals that aren't man or woman.