6

I've got a problem with deserialization of ActiveRecord serialize method in my tests.
It just gives me pure String instead of my data Array.

Example:

User > ActiveRecord::Base
   serialize :roles, Array
end

Console:

u = User.new
u.roles = ["admin", "support"]
u.save
# => true

User.first.roles
# => ["admin", "support"]

In test environment:

User.first.roles
# => "--- \n- admin\n- support\n"

it is a String. It seems that YAML serialize is not working there but I have no idea what to do to get it working.

I was already trying to figure it out with this, this, this and this, but with no luck.

Any advice would be greatly appreciated.
Martin

Community
  • 1
  • 1

2 Answers2

2

After all to bypass this I needed to call the data in database directly without involvement of "serialize" AR method.

YAML::load(User.first.roles_before_type_cast)

And now test environment also directly parses the data and give me correct roles Array.

0

I dont know if it solves your problem, but you can always get back the array from the string using

YAML::load("--- \n- admin\n- support\n")
# => ["admin", "support"]

So, in your test environment, you should do

YAML::load(User.first.roles)
rubyprince
  • 17,559
  • 11
  • 64
  • 104
  • It won't work because AR serialize method automatically uses the YAML parsing when trying to get data by: User.first.roles – Martin Seroczyński Mar 10 '11 at 14:57
  • Exactly, in test environment it returns just string but I cannot make the double YAML::load on object which works in development and production. It is dirty trick to make the YAML parsing by hand in second method in my object. – Martin Seroczyński Mar 10 '11 at 15:05