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.

ActiveRecord can't deserialize hashes with range keys

#3067

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)
  end
end

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

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

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

Activity

  1. Steve Richert
    Steve Richert

    I should mention that this is in Rails 2.3.3.

    August 18th, 2009 @ 09:38 PM

  2. Steve Richert
    Steve Richert

    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.

    August 18th, 2009 @ 10:19 PM

  3. Scott Steadman
    Scott Steadman

    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
    

    September 24th, 2009 @ 04:44 PM

  4. Rohit Arondekar
    Rohit Arondekar
    • 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.

    October 6th, 2010 @ 06:46 AM