Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top