Rails Update hstore column
For example if i have a hstore
column like below
settings public.hstore DEFAULT '"currency"=>"USD"'::public.hstore NOT NULL,
Now I will update the column to add preferences
in a migration as below
class AddPreferencesToUser < ActiveRecord::Migration[5.1] def change reversible do |dir| dir.up do update <<-SQL UPDATE users SET settings = settings || hstore('preferences
', ''); SQL end end end
Validation
Assume we want to validate preferences
to be one of facebook, twitter, linkedin these.
the validation looks this. as enum
doesn’t work with hstore key well.
store_accessor :preferences
validates :preferences
, inclusion: ['facebook', 'twitter', 'linkedin'], allow_blank: true
see you next time!