First, sorry for bad title, couldn’t come up with something more descriptive.
I have a user model. I’d like to set a type for that user which can be one of 5 options; doesn’t matter what the options are, any strings will do. I’d like to be able to search based on these options.
I think I have the following options:
- Added field as string to model with index
- Create a user_type table and do belongs_to/has_many relationship
- Save type as integer and map integer to a constant defined in user model, with string value (no idea how to do this though)
What’s the Rails Way to solve this problem that will meet the following goals:
- Searchable
- Maintainable
- Readable
Thank you
This really does depend on your meaning of ‘type’. If it’s a role (such as “administrator”, “moderator”, “user”, etc.) where a given user can only be of one type, you can just do it with a string column – no need for a separate table.
If the user can be multiple types simultaneously (“newly-registered”, “male”, “student”, “blogger”), where the user type works more like a tag, then your
user_typetable will do the job.