This project is archived and is in readonly mode.

#3067 ✓stale
Steve Richert

ActiveRecord can't deserialize hashes with range keys

Reported by Steve Richert | August 18th, 2009 @ 08:47 PM

AR seems able to serialize just about anything you give it, but it throws an error when given a hash that contains a ruby range as one of its keys.

class Student < ActiveRecord::Base
  validates_presence_of :age
  validates_numericality_of :age, :greater_than_or_equal_to => 0, :only_integer => true

  serialize :schools, Hash

  def school
    schools.detect{|k,v| k === age }.try(:last)

student = Student.create(:schools => {(5..10) => :elementary, (11..13) => :middle, (14..17) => :high}, :age => 6)

The creation works for me but when AR tries to deserialize the "schools" hash, I get the following error:

ActiveRecord::SerializationTypeMismatch: schools was supposed to be a Hash, but was a String

Comments and changes to this ticket

  • Steve Richert

    Steve Richert August 18th, 2009 @ 09:38 PM

    I should mention that this is in Rails 2.3.3.

  • Steve Richert

    Steve Richert August 18th, 2009 @ 10:19 PM

    Here's a patch that adds two tests:

    • A passing test for serializing a run of the mill symbol/string hash
    • A failing test attempting to deserialize a hash with a range key.
  • Scott Steadman

    Scott Steadman September 24th, 2009 @ 04:44 PM

    Actually this seems to be a problem with ruby:

    irb(main):001:0> require 'yaml'
    => true irb(main):002:0> YAML::load(YAML::dump({1..5 => 'foo'}))
    ArgumentError: syntax error on line 2, col -1:
    begin: 1<br/> end: 5 excl: false : foo


        from /usr/local/ruby-1.8.6/lib/ruby/1.8/yaml.rb:133:in `load'
        from /usr/local/ruby-1.8.6/lib/ruby/1.8/yaml.rb:133:in `load'
        from (irb):2
  • Rohit Arondekar

    Rohit Arondekar October 6th, 2010 @ 06:46 AM

    • State changed from “new” to “stale”
    • Importance changed from “” to “”

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

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>