views:

468

answers:

4

I have seen two different approaches in saving user preferences.

APPROACH 1: Serializing them and saving in one of the column of USERS table

APPROACH 2: Creating a separate table PREFERENCES and make a has_many association from USERS to PREFERENCES.

Which one of the above two approaches would you prefer and what are the pros and cons of each over other?

+2  A: 

Approach 2

You can add preferences, without cluttering up the user table

rikh
+9  A: 

It's usually a good idea to favor normalization. The second solution keeps your models cleaner, allows for easy extensibility if new preferences are added, and keeps your tables uncluttered.

Pesto
+1  A: 

There are some Rails plugins to handle this usecase:

  • Preference-fu (good for simple boolean preferences, uses a single column for multiple preferences)
  • Preferences (more flexible, uses a separate table, some nice syntactic sugar)
hopeless
+1  A: 

I'd approach 2 because it is cleaner and easier to update. You will be able to add more preferences as complex as you want.

It will be a bit slower since you have a join to do, but it'll be worth it

marcgg