Ruby on Rails 3 - Serialize Array Mismatched Class Bafflement

蓝咒 提交于 2019-12-11 07:28:34

问题


Hey there, I am baffled by this serialization mismatch problem in Rails 3.0.5 and Ruby 1.9.2. I am seeding the database with a subclass of Array and then trying to save to an ActiveRecord object. Can anyone please help me? I was initially trying to serialize as Graph but reduced it to Array to avoid bugs with the custom class. I am very stumped as this doesn't make intuitive sense to me. Thank you very much for your help!

class Graph < Array
  ..

class Settings < ActiveRecord::Base
  serialize :graphs, Array
  ..

Very last lines in seeds.rb -- eg nothing happens after. Just saving a lot for debugging purposes:

  sh_1g = sh_1g.to_a
  d.company.settings.add_graph(sh_1g.to_a)
  d.company.settings.save!
  d.company.save!  
  d.save!
  if sh_1g == d.company.settings.graphs[0]
    puts "the added graph matches the first graph in the graphs array"
  end
  puts "added " + sh_1g.inspect + " to " + d.company.settings.graphs.inspect
  puts "class of added graph as saved is" + d.company.settings.graphs[0].class.inspect
  puts "class of added graph is " + sh_1g.class.inspect
  puts "class of graphs serial is " + d.company.settings.graphs.class.inspect

Output of seeds.rb puts as it is run:

the added graph matches the first graph in the graphs array
added [[[0, 0, ..]]] to to [[[[0, 0, ..]]]..]
class of added graph as saved isArray
class of added graph is Array
class of graphs serial is Array

However, in console:

ruby-1.9.2-p180 :002 > Company.all[1].settings
ActiveRecord::SerializationTypeMismatch: graphs was supposed to be a Array, but was a String
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/attribute_methods/read.rb:106:in `unserialize_attribute'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/attribute_methods/read.rb:82:in `read_attribute'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/base.rb:1586:in `attribute_for_inspect'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/base.rb:1667:in `block in inspect'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/base.rb:1665:in `collect'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/base.rb:1665:in `inspect'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:146:in `inspect'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
    from /Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

回答1:


I was having a problem similar to this one. My problem stemmed from the placement of my serialize :some_column, Array statement. If by some chance the attribute methods on your ActiveRecord object class get defined before you call serialize, you will not have the setter and getter methods properly defined.

So, I would make sure that the define_attribute_methods is not being called on your Settings class before you declare which columns are serialized.




回答2:


set option

:null => true

in your migration for the field "graphs". It fixes this error



来源:https://stackoverflow.com/questions/5156900/ruby-on-rails-3-serialize-array-mismatched-class-bafflement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!