问题描述
嘿,我对这个序列不匹配的问题,Rails的3.0.5和Ruby 1.9.2百思不得其解。我播种与阵列的一个子类的数据库,然后试图保存到一个ActiveRecord对象。谁能帮帮我吗?我最初是想序列化为图形,但它降低到阵列,以避免错误与自定义类。我很为难,因为这没有任何直观的感觉来了我。非常感谢您的帮助!
类图<排列
..
一流的设置和LT;的ActiveRecord :: Base的
连载:图表,阵列
..
在seeds.rb
非常最后几行 - 如没有之后发生的。只是节省了大量用于调试的目的:
sh_1g = sh_1g.to_a
d.company.settings.add_graph(sh_1g.to_a)
d.company.settings.save!
d.company.save!
d.save!
如果sh_1g == d.company.settings.graphs [0]
把所添加的图形的第一图表中的图表阵列匹配
结束
提出增加了+ sh_1g.inspect +到+ d.company.settings.graphs.inspect
把类保存添加图形是+ d.company.settings.graphs [0] .class.inspect
把类加图的是+ sh_1g.class.inspect
把类图序列的是+ d.company.settings.graphs.class.inspect
seeds.rb输出所说,因为它是运行:
在图形数组中的第一个图形中添加图形匹配
加入[[[0,0,..]]]向[[[[0,0,..]]] ..
班加图作为保存IsArray的
班加图的是数组
类图系列是数组
不过,在控制台:
红宝石-1.9.2-P180:002> Company.all [1] .settings
ActiveRecord的:: SerializationTypeMismatch:图应该是一个数组,而是一个字符串
从/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
从/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
从/Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/base.rb:1586:in`attribute_for_inspect
从/Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/base.rb:1667:in`块检查
从/Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/base.rb:1665:in'收集'
从/Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/base.rb:1665:in'检查'
从/Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:146:in '检查'
从/Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in'开始'
从/Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in'开始'
从/Users/dlipa/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.5/lib/rails/commands.rb:23:in`<顶(必填)>
从脚本/导轨:6:在'要求'
从脚本/导轨:6:`<主>
我有一个与此相似的问题。 some_column,阵列语句:从我的位置连载了我的问题源于。如果由于某种在你的ActiveRecord对象类属性的方法获得定义在打电话之前
连载
,你不会有正确定义的setter和getter方法。
所以,我会确保该 define_attribute_methods
不会被调用您的设置类,你声明的列进行序列化之前。
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>'
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.
这篇关于Ruby on Rails的3 - 序列化阵列不匹配类的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!