Making Rails' Serialize Even Better
Rails has this handy method that allows you store almost any object in the database with ease. Most often I end up using it for storing optional attributes in a hash.
Here is the proper syntax for telling Rails that there is an options attribute that should only store Hash values.
class User < ActiveRecord::Base
serialize :options, Hash
end
The problem
The options attribute will start off as nil, and remain nil until you set it to something else. Setting the class_name to Hash only affects what you can write to this attribute.
>> user = User.new
=> #<User id: nil, name: nil, options: nil>
>> user.options[:theme]
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
from (irb):2
=> nil
The solution
What we really need is to automatically return an empty Hash on this new object so we can go on our merry way.
Add this to your environment.rb.
config.gem 'jqr-typed_serialize',
:lib => 'typed_serialize',
:source => 'http://gems.github.com'
Now run this command to install the gem.
$ rake gems:install
A quick change of our model will fix all of our woes.
class User < ActiveRecord::Base
typed_serialize :options, Hash
end
>> user = User.new
=> #<User id: nil, name: nil, options: nil>
>> user.options[:theme]
=> nil
>> user.options
=> {}
本文介绍了一种改进Rails中序列化属性的方法,通过使用typed_serialize插件可以为特定属性指定默认值,避免nil对象引发错误,并提供了安装及配置教程。
288

被折叠的 条评论
为什么被折叠?



