Where would you store a users preferences in a Rails app?
Should I put it in the User Model or just use serialize
Where would you store a users preferences in a Rails app?
Should I put it in the User Model or just use serialize
This is really dependent of your app. How big is it, will it grow in the future, how many settings are there, will the number of settings increase, etc.
If you have only a few settings (my personal limit is 5) you can probably save them in your User model. Pros: Easy to implement. Cons: New settings need changes in the database, i.e. migrations every time you add a new one or change an existing one.
If you have more settings, and they going to change or increase in number, you probably have an easier time saving them in their own model. The best way is to use some kind of existing Key-Value storage, or faking your own. Pro: You can easily add new settings, or change existent. Cons: Harder to implement, can be overkill for a small app.
You could also put all settings in a single attribute and just serialize that attribute in the model. It'll basically contain a hash of any number of user settings that you want. It'll be easy to add/remove them as you go.
example:
User model
serialize :settings get_settings(type => :filter) end set_settings(type => :filter, data => {}) end User table
text settings User Row
:filter: :statuses: - "status 1" - "status 2" :pagination :per_page: 20 :last_viewed_page: 10 You can use it like these:
set_settings(:pagination, {:per_page => 10, :last_viewed_page => 2}) user_pagination = get_settings(:pagination, {:pagination} user_pagination[:per_page] user_pagination[:last_viewed_page]