I'm looking to learn how to serialize data in Rails 3...
Things I'd like to learn:
Thanks!
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.
Ruby Topics Serialization is the process of writing data of an object to a stream. The inverse process of reading stream into an object is called Deserialization. This is very useful when you want to preserve state of an object, and then retrieve it later. Serialization can be done in Ruby via a class called Marshal.
Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.
ActiveRecord has this build in: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-serialize
If you specify in your model that an attribute is serialized, then AR transparently handles the serialization/deserialization. When you call the accessor, it'll deserialize on the fly return the Ruby object (hash, array, whatever). Then you can modify the object as you would normally, and when you save the record, it'll re-serialize and store it.
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