We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Mar Elias • 11 years ago

Thanks for the article, though there is in my opinion a problem dealing with hstore is that when you store it in the database as hash. It's really hard to retrieve the object as hash because it will get back as a string with hashes inside it. So I need to evaluate the different hashes inside the "preferences" for example differently and use most of times 'eval' when I load the object again. Also defining attributes types will work for some types like boolean, but for other types like if 'newsletter' was of Hash[Symbol => Integer] type, calling user.preferences.newsletter.class will return a string and not HashClass.
Additionally that serializing using Virtus is just useful in this case in seperating the logic and avoid using store_accessor(Prevent user class from exposing what is inside the preferences hash). But when retrieving the object it will be considered as a normal hstore and I am not sure how virtus will be usefull.

In another word I find out when your hstore object get much complicated, like having nested hashes and arrays. I think It's better consider another solution !

Adrien Bourgeois • 10 years ago

Finally someone that explains how to query the VALUE of a hstore! Great stuff, thanks mate

Guayo Mena • 10 years ago

How can I make a scopes instead of queries those queries?

I tried something like:
scope : newsletter, -> (x) { where preferences[: newsletter] x}

But I can't get it to work :/

Nando Vieira • 10 years ago

Probably something like this:

scope :newsletter, lambda do |subscribed|
where("preferences @> hstore('newsletter', ?)", subscribed.to_s)
end
Guayo Mena • 10 years ago

I returns
tried to create Proc object without a block

Shouvik Mukherjee • 9 years ago

Great writeup. Thank you very much!

One thing I noticed, if you are using schemas in PostgreSQL, you have to add the schema on the search path where the hstore extension is enabled in order to add columns with 'hstore' data type on other schemas.

Ali Ahmed • 8 years ago

Awesome

uzaif • 7 years ago

Thanks for the article,
I have one question.
how can i permit serialize :preferences, UserPreferences in controller