Serializing an AR object into an attribute in another


I don't know if this is a bug or it is the expected behavior but I am
trying to serialize an AR object into another.
I know this is not very usual but I don't want to use a belongs_to
association because the object is transient.
I need an snapshot of how the 2nd object was at the moment of the
creation of the 1st.

class One < ActiveRecord::Base

class Two < ActiveRecord::Base
  serialize one

obj1 = One.find(:first)
obj2 = Two.create(...) = obj1

My problem is that it seems that AectiveRecord only saves the id of the
serialized object when create or update the second object and not all
the yaml format of the serialized.
There is not any association between the two objects.

Anyone knows if I am on the right track? Or I need to change the

Juanma Cervera

You might want to try doing your own "serializing".

class Two < ActiveRecord::Base
  def one=(new_one)
    self.write_attribute(:one, new_one.to_yaml)
  def one
    @one ||= YAML.load(self.read_attribute(:one))

I haven't tested this, but it might be a place to start.

Juanma Cervera wrote:

It has been easy with your help.
Many thanks.