hstore_accessor
hstore_accessor copied to clipboard
With hstore_accessor is it possible to pluck an hstore field?
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]