0

I have rails model that can have three values a, b, and c.

It should be possible to assign none of them, one, of them, two of them or all of them.

My idea is that I can use this attribute as a string, and serialize it to use it as an array (for example [a, c], or [] or [a,b,c]. Using some strong validations it seems to me the logic way to go and I know rails can handle it.

Anyway I read everywhere that an array attribute is a wrong choice and that I should use an has_many relation. Is there a good reason to have an has_many relation or it's an overkill for just a, b and c?

Another option would be to add three boolean attributes to my table (a:boolean, b:boolean, c:boolean) and work on that logic.

What is the best way to take for my scenario?

1 Answer 1

1

Rails 4 comes with native support for postgresql's array data type. So if you are using postgresql then you should use the array datatype instead of creating another model with has_many relationship.Although array datatype is not as fast as integer or string but considering the joins you have to perform it will give you some performance improvement.

Sign up to request clarification or add additional context in comments.

2 Comments

And what about array vs boolean fields?
@TopperH Boolean data types are obviously faster than array.You can add three different column to the db table but if you consider situations where 1000 users select only first field then the two other column in your db table will be empty which is not a good design principle. So you should go with a has_many relationship (if you don't mind performing joins) or use a array type or serialize a array in a string column(rails 3 way)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.