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?
I grappled with this same question so I thought I'd share what I found in a "community wiki" answer.
Serializing in a single attribute
Simple user preferences for your Rails app is a blog post describing how to do this.
Edit a serialized hash in a form? describes how to edit such a hash in a form. A helpful trick is to make the form from
OpenStruct.new(@user.preferences)
hash to automatically make accessor methods for each hash attribute.DYE/has_serialized - GitHub lets you treat those attributes in the serialized hash as attributes on the (user) model.
Preferences in a separate table
Best practice to store user settings? has some tips. Below are some libs including two from another answer by @hopeless.
You can also try using metaprogramming: Practical Metaprogramming with Ruby: Storing Preferences