views:

439

answers:

2

What is the difference between Marshalling and ActiveRecord Serialization?

Is there any particular occasion when it is preferable to use one over the other to save an object to the database?

+1  A: 

Marhshalling and serializing are both general terms that describe the same basic principle. By definition anything process that is able to encode an object and its dependent substructures into something that can be saved such that at some later point it can be restored is termed serializing or marshalling.

There's a bit of clarification on how these terms can differ, but are typically the same, in the Wikipedia entry: http://en.wikipedia.org/wiki/Marshalling%5F%28computer%5Fscience%29#Comparison%5Fwith%5Fserialization

The specific difference in terms of Ruby is that the built-in module Marshal is available for any application, whereas ActiveRecord serialization support is specific to the Rails platform.

Since ActiveRecord has its own tightly integrated serialization method, it is generally preferable to use that.

tadman
I found out that ActiveRecord serialization is really slow compared to Marhal.
collimarco
+1  A: 

IIRC:

Ruby Marshall is not guaranteed to work across different ruby versions or the same ruby versions on different platforms.

Because you may have different Ruby versions accessing the same serialized column, Rails implements it's serialization using YAML. Whilst this is slower, it does guarantee your serialized column can be read by other ruby versions, ruby on other OSs and also other programming languages.

Olly