I have an application that was working fine with ror 2.3.x. I am having trouble upgrading to Rails 3 with serialization.
The code looks like this
class PaymentTransaction < ActiveRecord::Base
serialize :response
end
The response is开发者_StackOverflow supposed to contain the ActiveMerchant::Billing::Response. With rails 3 for some reason its being save as string.
=> #<PaymentTransaction id: 11, order_id: nil, amount: nil, mode: nil, payment_profile_id: nil, response: "#<ActiveMerchant::Billing::Response:0x1051aec98>", created_at: "2010-11-07 04:06:03", updated_at: "2010-11-07 04:24:58", result: "pending", payee: nil, login_id: nil, transaction_key: nil>
I didn't any notes on serialization in any other blogs talking about upgrade. Any thoughts?
The Rails 2 explanations for using serialization did not work in Rails 3 for me unless I also specified the type of the serialized object in the serialize call. For example:
serialize :response, Array
After specifying array the functionality worked as expected.
Further documentation here:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html
under "Saving Arrays [...]"
There was a small change in rails 3 which have an effect: https://github.com/rails/rails/commit/c1d73270717f30498f8f4d55d6695509107c2834
There are two good blog posts about serialization here:
- http://www.simonecarletti.com/blog/2010/04/inside-ruby-on-rails-serializing-ruby-objects-with-json/
- http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/
精彩评论