Rails: dynamic columns/attributes on models? Rails: dynamic columns/attributes on models? database database

Rails: dynamic columns/attributes on models?


To me this sound like a perfect example were you want to use a schema free NoSQL database.

Or (if you want to stick with SQL) you can have a base User model with a has_many :attributes association. In which a attribute is just a combination of a key ('age', 'birthday', 'hair color') and a value. Complexity comes with different datatypes and queries covering multiple attributes at the same time.


If you're using Postgresql you can take a look at hstore then you can save the information serialized and actually you can make some queries against those hashes btw Rails 4 has included this feature but if you are using an older Rails version you can include this gem. https://github.com/diogob/activerecord-postgres-hstore in your Gemfile and you should be able to start playing like:

user = User.newuser.preferences = {  email: "p@elh.mx",  github: "heridev"}user.save!user.reload# SearchingUser.where("preferences @> hstore(:key, :value)", key: "email", value: "p@elh.mx").first