关于rails 序列化Hash的问题

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
=> {}
我使用上面方法出现安装出现问题, 后来发现可以用 gem install typed_serialize 解决

你可能感兴趣的:(UP,Rails,ActiveRecord,Go)