Skip to content

Instantly share code, notes, and snippets.

@dinks
Created November 4, 2013 11:40
Show Gist options
  • Save dinks/7301370 to your computer and use it in GitHub Desktop.
Save dinks/7301370 to your computer and use it in GitHub Desktop.
Unserialize With Encoding Monkey Patch
# From http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3
class ActiveRecord::Base
def unserialize_attribute_with_utf8(attr_name)
traverse = lambda do |object, block|
if object.kind_of?(Hash)
object.each_value { |o| traverse.call(o, block) }
elsif object.kind_of?(Array)
object.each { |o| traverse.call(o, block) }
else
block.call(object)
end
object
end
force_encoding = lambda do |o|
o.force_encoding(Encoding::UTF_8) if o.respond_to?(:force_encoding)
end
value = unserialize_attribute_without_utf8(attr_name)
traverse.call(value, force_encoding)
end
alias_method_chain :unserialize_attribute, :utf8
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment