1

Where would you store a users preferences in a Rails app?

Should I put it in the User Model or just use serialize

2
  • You can store in user model that way they can be editable Commented Aug 3, 2011 at 10:42
  • What's the difference? You must hold in the User Model and I think , is the best practice serialize it. ( except if this information is excessively big) Commented Aug 3, 2011 at 16:54

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.