user profiles lookup-table values as constants (denormalised) or models (normalised)
My site contains user profiles with fields such as hair and eye color.
I have been implementing lookup-table type fields with constants in my user model, ie.
HAIR = %w[shaved black blond brown red grey white bald]
EYES = %w[black brown blue hazel green grey other]
used in my views with collection_select statements to populate drop down lists. I have done this with constants is to avoid ~10 additional joins each time a user profile is viewed.
<%= f.collection_select :eyes, Profile::EYES, :to_s, .....
Values are stored开发者_运维百科 as integers in the model, ie. profile.hair = Profile::HAIR.index("red")
Am I at any obvious disadvantage not storing this data in models (eg. Eye model, Hair model) - will this have any great negative impact on search speed if I wish to perform searches based on eye = blue, hair = black for example?
Thanks
This is a great example of where ActiveHash can be used:
http://github.com/zilkey/active_hash
Let's you create nice in-memory enumeration models to track these things, which will let you create meaningful associations in your models.
It shouldn't have too much impact on search speed for a reasonable amount of data. Although you should make sure you define SQL indexes on hair and eyes columns to make it easier for database to perform a search.
精彩评论