Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Serialize

I'm looking to learn how to serialize data in Rails 3...

Things I'd like to learn:

  • A. What serialized data looks like (example for photo_id: 123123, photo_name: "Blah)
  • B. How to serialize data in Rails 3
  • C. How to extract serialized data to do something like mydate.photo_id given the above.

Thanks!

like image 837
AnApprentice Avatar asked Sep 30 '10 21:09

AnApprentice


People also ask

What is serialize in Rails model?

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.

What is serializing in Ruby?

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.

What serialization means?

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.


1 Answers

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.

like image 104
tfe Avatar answered Sep 18 '22 22:09

tfe