How do I tell Ruby not to serialize the attribute or how to properly overload marshal_dump?

I have an attribute in my AR: B that is not serializable.

o = Discussion.find(6)
Marshal.dump(o)

TypeError: no marshal_dump is defined for class Proc
       from (irb):10:in `dump'

      

I know the culprit and want this variable to be zero before serialization happens.

I can do this, but I am stuck on the correct way to override marshal_dump

 def marshal_dump
   @problem = nil
   # what is the right return here?
 end

      

Or is there a way to tell Ruby or AR not to serialize the object?

+2


a source to share


1 answer


Your custom one marshal_dump

should return an object containing the data you want to serialize. This object will be returned to marshal_load

at boot time.

In this case, I am assuming that the data you want to dump matches all AR attributes (and only those), so I would try:



def marshal_dump
  attributes
end

def marshal_load(data)
  send :attributes=, data, false  # false to override even protected attributes
end

      

+2


a source







All Articles