serialized checkboxes in Rails
I would like to add 7 checkboxes (boolean values) to one of my Models.
Is there a way to make something like an array of bools in my db, instead of adding 7 different columns by hand?
Oh I'm using the开发者_JAVA百科 pg database
Depending on how often do you want those bool to be edited (in a form in ajax etc.). You may be better off staying with individual columns. Simply because the f.check_box :column_name versus f.object.column_name.each_pair ... The similar situation in the controller when posting them back in. Only good reason I can think of to add serialized 7 checkboxes in a hash and store them in one column is if your 7 checkboxes can be changed dynamically and such.
Yes.
CREATE TABLE array_test (
bools bool[]
);
Not saying I recommend this approach. Usually you are better with a mapping table or multiple columns for reasons of indexing, semantic clarity, and enforcing proper data integrity. However, if you are weighing this I assume you know the hazards or else you will learn your lesson. There are some rare cases where this makes sense however....
精彩评论