Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

OrderedHash serialization does not work when storing arrays

#6646

I'm storing an OrderedHash containing A/R validation errors in a serialized column. When I retreive it from the database the array of errors become a string containing the last item of the array.

Model:

class InvalidRecord < ActiveRecord::Base
  belongs_to :record, :polymorphic => true
  serialize :validation_errors
end

Schema:

  create_table "invalid_records", :force => true do |t|
    t.integer  "record_id",         :null => false
    t.string   "record_type",       :null => false
    t.text     "validation_errors"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

The following code...

  r = InvalidRecord.new(:record => invalid_user, :validation_errors => invalid_user.errors)
  p r
  r.save!
  r.reload
  p r

outputs:

# new record: (errors on username is an array containing 2 items)
#<InvalidRecord id: nil, record_id: 1, record_type: "User", 
validation_errors: #<OrderedHash {:username=>["can't be blank", "is too short (minimum is 3 characters)"]}>, 
created_at: nil, updated_at: nil>

# saved record: (errors on username is now a string)
#<InvalidRecord id: 2, record_id: 1, record_type: "User", 
validation_errors: #<OrderedHash {:username=>"is too short (minimum is 3 characters)"}>, 
created_at: "2011-03-29 22:08:24", updated_at: "2011-03-29 22:08:26">

I'm using ree-1.8.7-2010.02 [ x86_64 ] with sqlite3.

Here is the YAML stored in the db:

--- !omap 
   - :username: can't be blank
   - :username: is too short (minimum is 3 characters)

Reported by Philippe Creux · March 29th, 2011 @ 11:21 PM

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

No comments yet.