madeintandem / hstore_accessor

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

With hstore_accessor is it possible to pluck an hstore field? #74

Open axelson opened 7 years ago

axelson commented 7 years ago

Say I have a User model with an hstore field called settings that contains a boolean called notifications_enabled. That allows me to do

[34] pry(main)> User.last.notifications_enabled
=> true

But if I didn't want to load the User model into an instance is there a way to utilize hstore_accessor to pluck out just the reminders_enabled value? I can currently do something like:

[35] pry(main)> User.where(id: 1).pluck("settings->'notifications_enabled'")
=> ["true"]

But that is less than ideal because we lose the translation of the string "true" to the value true and I'd rather not hardcode the Postgres "settings->'notifications_enabled'" query.

Ideally we could do something like:

User.where(id: 1).pluck(:notifications_enabled)
=> [true]