In some ruby on rails projects, I have seen instances where strings are used instead of a foreign key reference to a lookup table.
I usually code in C#/SQL Server and use lookup tables, but am not particularly experienced in ruby on rails.
So my question is, should I be using strings instead of lookup tables in my ruby on rails web project (mysql and active record)? Is this something specific to ruby on rails, or to dynamically typed languages? Or for some particular circumstances? Or is it just bad database design?
By lookup tables I mean, like the following
UserStatus
Id Name 1 Active 2 Disabled
User
Id Username UserStatusId 1 john.smith@gmail.com 1 1 jane.smith@gmail.com 2
Whereas with just strings
User
Id Username UserStatus 1 john.smith@gmail.com active 1 jane.smith@gmail.com disabled
Best Answer
[EDIT] Ruby on Rails 4 enums
Rails 4 supports enums, so you can declare an enum attribute where the values map to integers in the database, but can be queried by name.
Rails 3 and older
You could implement something like this:
You would use it like this:
Will return:
Note: I could have used
'active'
instead of:active
but in this case, use of symbols is more appropriate.