I have an instance variable in an active record class called hash_value
. It's a serialized hash.
I want to display the hash as XML. Is it right to call hash_value.to_xml
? Many nodes are numbers, and XML tags are not allowed to be only number (or start with a number).
I want to override the to_xml
method of hash_value
. I don't want to override on all hashes, just the hash that's in this record.
class ProductVersion < ActiveRecord::base
serialize :hash_value, Hash
def hash_value.to_xml
end
end
I tried the answer here redefining a single ruby method on a single instance with a lambda
but it doesn't seem to be applicable. I suspect because when I load the record, it creates a new hash_value
object and thus the singleton adjustment on the original is moot.
Any thoughts would be great. I know I could write a function hash_value_to_xml, but I'd rather avoid doing something like that.
Thanks to the first comment, I came up with a solution. Not a good one, but one that works. I'd love to see if there's a better way, because this one smells a bit.
class MyHash < Hash
def to_xml
1/0 #to see if it's run.
end
end
def hash_value
MyHash.new().merge( attributes['hash_value'] );
end