Skip to content

Instantly share code, notes, and snippets.

@jonsgreen
Created July 27, 2012 16:52
Show Gist options
  • Save jonsgreen/3189117 to your computer and use it in GitHub Desktop.
Save jonsgreen/3189117 to your computer and use it in GitHub Desktop.
module HstoreSerializer
extend ActiveSupport::Concern
module ClassMethods def hstore_array(*attributes)
attributes.each do |attr|
define_method "#{attr}=" do |values|
self[attr] = convert_to_hash(values)
end
define_method attr do
self[attr].try(:keys) || []
end
end
end
end
private
def convert_to_hash(values)
values.select(&:present?).inject({}) {|hash, s| hash.merge({ s => true}) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment