Skip to content

Instantly share code, notes, and snippets.

@andikan
Forked from Electron-libre/pg_array_hsotre_fix.rb
Created January 25, 2014 16:07
Show Gist options
  • Save andikan/8618643 to your computer and use it in GitHub Desktop.
Save andikan/8618643 to your computer and use it in GitHub Desktop.
module PgArrayHstoreFix
def self.included(base)
base.class_eval do
before_save :serialize_array_hash
end
def serialize_array_hash
self.class.attribute_names.each do |attribute|
column_definition = self.column_for_attribute(attribute)
if column_definition.array && column_definition.type == :hstore
serialized_hstore_array = []
self.send(attribute.to_sym).each do |value|
serialized_hstore_array << value.to_s.gsub(/{|}/,'')
end
self.send("#{attribute}=".to_sym, serialized_hstore_array)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment