Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Serialization issue

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 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?

like image 517
Addy Avatar asked Nov 07 '10 04:11

Addy


2 Answers

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 [...]"

like image 94
tks Avatar answered Sep 24 '22 04:09

tks


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/
like image 37
Andrew Nesbitt Avatar answered Sep 24 '22 04:09

Andrew Nesbitt