Marshaling Objects with ActiveRecord Objects in them

I have a hash with ActiveRecord objects in them that I am marshaling and
storing as a constant in my migration files–so I can store additional
information needed while I do migrations. I am able to do migrations
just fine, which tells me that adding the constant (which is equal to
them marshaled hash) to the migrate class doesn’t cause any problems.

However, when I try to do a Marshal.load(MigrateClass::HASH_OBJ) I get
the following error:
undefined class/module MyActiveRecordClassName

…where MyActiveRecordClassName is the first ActiveRecord reference
made in the marshaled hash. I know that objects that “include bindings,
procedure or method objects, instances of class IO, or singleton
objects” (see ruby docs) cannot be marshaled. Do ActiveRecord objects
include any of these? Can ActiveRecord objects be marshaled?

Caleb wrote:

I know that objects that “include bindings,
procedure or method objects, instances of class IO, or singleton
objects” (see ruby docs) cannot be marshaled. Do ActiveRecord objects
include any of these? Can ActiveRecord objects be marshaled?

I’m pretty sure that ActiveRecord objects CAN be marshaled. I am able
to do the following running ‘./script/console’ w/o any errors:

obj1 = MyActiveRecordClass.find_by_id(1)
obj2 = Marshal.load(Marshal.dump(obj1))
obj1 == obj2
=> true

I get the above “undefined class/module MyActiveRecordClassName” error
even though my $: path variable contains a reference to my ‘models’
directory where all of my active record classes are defined. Any ideas?

They can be, try adding

model :MyActiveRecordClassName

to the ApplicationController

Vish

Vishnu G. wrote:

They can be, try adding
model :MyActiveRecordClassName
to the ApplicationController

Thanks Vish! That did the trick! Note that I had to use the
unserscore-lowercase version of the class names for the model symbols.
Example:

model :my_active_record_class_name

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs