serialize'd array not unserializing properly
Reported by auzigog | July 17th, 2008 @ 10:32 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
"foo"
end
end
class Board < ActiveRecord::Base
serialize :pegs
def setup_pegs
self.pegs = [Peg.new, Peg.new, Peg.new]
end
end
class SomeController < ApplicationController
def index
b = find_or_create_board
b.pegs[0].some_method # IMPORTANT LINE!
end
def find_or_create_board
b = Board.find(:first)
if b.nil?
b = Board.new
b.setup_pegs
b.save!
b
end
end
end
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
-

auzigog July 17th, 2008 @ 10:34 PM
Sorry, those #'s in the middle were supposed to look like this:
"#<YAML::Object:0x52cc220 @ivars={}, @class="Peg">",
"#<YAML::Object:0x52cc220 @ivars={}, @class="Peg">",
"#<YAML::Object:0x52cc220 @ivars={}, @class="Peg">"
-
-

auzigog July 17th, 2008 @ 10:39 PM
Because Peg is not a model? It doesn't deserve to be a model. There are hundreds of pegs per row and there is no point in wasting tons of database rows with simple objects that could easily be serialized instead.
-

-

-
-

auzigog July 18th, 2008 @ 08:06 PM
Am I wrong in assuming that serialize isn't working as expected?
Using composed_of doesn't resolve that unexpected behavior, it just fixed my problem.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
Repository is at http://github.com/rails/rails
Check out the development master (Edge Rails):
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..
