Rails: Best practice to store user settings?

后端 未结 5 966
不知归路
不知归路 2020-12-14 07:43

I\'m wondering what the best way is to store user settings? For a web 2.0 app I want users to be able to select certain settings. At the moment is it only when to receive em

5条回答
  •  感情败类
    2020-12-14 08:27

    If you use PostgreSQL, the best solution is to use https://github.com/diogob/activerecord-postgres-hstore/. It's a simple, fast and reliable way to store hashes in the database. Since it's not just a serialised text field, you can search on it also, and you don't need to create a new table, as in HasEasy.

    def User
      serialize :preferences, ActiveRecord::Coders::Hstore
    end
    
    user = User.create preferences: { theme: "navy" }
    user.preferences['theme']
    

提交回复
热议问题