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?
ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With