madeintandem / hstore_accessor

Adds typed hstore-backed field support to ActiveRecord models.
MIT License
242 stars 47 forks source link

Querying Hash Type? #27

Closed peck closed 10 years ago

peck commented 10 years ago

Is it possible/beneficial to have a macro for querying the hash type?

I'm trying to figure out how to do it now, coming up unsuccessful and wondering if this is even possible to query with postgresql.

I have a User with a hstore_accessor :fee_list, :hash type, where fee_list is a hash like:

{"pickup dry cleaning" -> 15, "walk dog" -> 20}

Is it possible to query for Users where "pickup_dry_cleaning" is a key in the fee_list? And even better if I could query on the value as well.

I'm fine dropping down to SQL directly to do this in need be, but having trouble figuring out how that would look as, so far as I can tell, you can't cast a result to a hash.

Thanks

tonycoco commented 10 years ago

It's pretty difficult to do... with my attempt at the 2.0.0 PR #40 this won't even be close to possible without a crazy regex. We've discussed this internally at DevMynd and decided that if you are needing to query that hash attribute of the hstore column it should most likely be its own table or hstore column all together.