Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Serializing objects in a database?

I'm looking for some general guidance on serializing objects in a database.

  1. What are serialized objects?
  2. What are some best-practice scenarios for serializing objects in a DB?
  3. What attributes do you use when creating the column in the DB so you can use a serialized object?
  4. How to save a serialized object?
  5. And how to access the serialized object and its attributes? (Using hashes?)
like image 227
keruilin Avatar asked Jun 02 '10 16:06

keruilin


People also ask

What does rails serialize do?

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 Ruby serialize?

Ruby Developer Resources 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.

What does it mean to serialize data in JavaScript?

The process whereby an object or data structure is translated into a format suitable for transfer over a network, or storage (e.g. in an array buffer or file format). In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() .


2 Answers

In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file, a memory buffer, or transmitted across a network connection link to be "resurrected" later in the same or another computer environment. (see http://en.wikipedia.org/wiki/Serialization)

  1. So serialized objects (in the context of ActiveRecord) are text/string representations of objects (encoded using YAML). When serialized, you can save (almost) any Ruby object in a single database field.

  2. You can use serialization if you have somewhat complex objects that you need to save in a database and you don't need to retrieve records based on the contents of a serialized attribute. I used them for example for storing preferences for users of a webapp: the preferences were basically hashes that I wanted to save in a single db field.

3./4./5. Use ActiveRecord::Base.serialize as Marc-André Lafortune suggested:

class User < ActiveRecord::Base   serialize :preferences end  u = User.new u.preferences = {:show_tooltips => true, :use_extended_menu => false, ...} u.save  # ...  u = User.find(23) u.preferences # => {:show_tooltips => true, :use_extended_menu => false, ...} 
like image 90
severin Avatar answered Sep 30 '22 01:09

severin


Really easy. Check the doc for ActiveRecord::Base.serialize.

like image 22
Marc-André Lafortune Avatar answered Sep 30 '22 03:09

Marc-André Lafortune