This project is archived and is in readonly mode.

#647 ✓invalid

serialize'd array not unserializing properly

Reported by auzigog | July 18th, 2008 @ 08:14 PM | in 2.x

I don't have time to make a patch for this, but I wanted to bring it to everyone's attention.

Take this code as an example

class Peg
  # does not inherit from ActiveRecord
  def some_method

class Board < ActiveRecord::Base
  serialize :pegs
  def setup_pegs
    self.pegs = [,,]

class SomeController < ApplicationController
  def index
    b = find_or_create_board
    b.pegs[0].some_method  # IMPORTANT LINE!
  def find_or_create_board
    b = Board.find(:first)
    if b.nil?
      b =

This code will work fine the first time though because it creates new Peg objects. The second time through, when it has to load the pegs from the database, the "b.pegs[0].some_method" will result in a NoMethodError because the "pegs" array actually looks (something) like this:

"#<YAML::Object:0x52cc220 @ivars={}, @class="Peg">",

"#<YAML::Object:0x52cc220 @ivars={}, @class="Peg">",

"#<YAML::Object:0x52cc220 @ivars={}, @class="Peg">"

It is an array of YAML objects instead of an array of Pegs, therefor, it doesn't have "some_method" like you would expect. I learned form this helpful blog that adding

require 'peg'

to the top of the Board class will solve this problem. The reason it is happening is because the YAML loading method can't find the Peg class and defaults to making it a YAML object.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket