Created
November 7, 2013 07:59
-
-
Save Electron-libre/7350792 to your computer and use it in GitHub Desktop.
Include this snippet into your model in order to use hstore in array with postgres and rails 4.0 ( fix is pending on the rails side )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
How I use this snippet?
Slap this file somewhere it will be loaded, like ./initializers
Then do include PgArrayHstoreFix
on your model. However, this patch doesn't quite do what we want. Nothing happens for deserialization so you just get key/value strings back instead of Ruby Hash objects.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool, thank you!