hstore_accessor icon indicating copy to clipboard operation
hstore_accessor copied to clipboard

With hstore_accessor is it possible to pluck an hstore field?

Open axelson opened this issue 8 years ago • 0 comments

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]

axelson avatar Feb 03 '17 18:02 axelson