Lookup-table user profiles as constants (denormalised) or models (normalized)

My site contains user profiles with fields such as hair and eye color.

I use lookup-table type fields with constants in my user model, i.e.

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 dropdowns. I did this with constants to avoid ~ 10 extra joins each time a user profile is viewed.

<%= f.collection_select :eyes, Profile::EYES, :to_s,  .....

Values ​​are stored as integers in the model, i.e. profile.hair = Profile :: HAIR.index ("red")

Is there any obvious drawback without storing this data in models (for example, Eye model, Hair model) - will this have a big negative impact on the search speed if I want to perform a search based on eye = blue, hair = black for example ?

thank

+3
source share
2 answers

This is a great example of using ActiveHash:

http://github.com/zilkey/active_hash

Allow you to create nice in-memory enumeration models to track these things, which will allow you to create meaningful associations in your models.

+1
source

. , SQL , .

0

Source: https://habr.com/ru/post/1753685/


All Articles