Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshalling vs ActiveRecord Serialization in Ruby On Rails

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?

like image 424
collimarco Avatar asked Aug 19 '09 16:08

collimarco


People also ask

Is ActiveRecord an ORM?

ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.

What is ActiveRecord :: Base in Rails?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is marshal in Ruby?

The Marshal Module As Ruby is a fully object oriented programming language, it provides a way to serialize and store objects using the Marshall module in its standard library. It allows you to serialize an object to a byte stream that can be stored and deserialized in another Ruby process.

What is serialization in Ruby on Rails?

Serialization converts an object in memory into a stream of bytes that can be recreated when needed. Serializers in Ruby on Rails convert a given object into a JSON format. Serializers control the particular attributes rendered when an object or model is converted into a JSON format.


1 Answers

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.

like image 175
Olly Avatar answered Sep 20 '22 12:09

Olly