Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveRecord serialize method doesn't work in test environment

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

like image 395
Martin Seroczyński Avatar asked Mar 09 '11 18:03

Martin Seroczyński


1 Answers

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.

like image 135
Martin Seroczyński Avatar answered Oct 14 '22 02:10

Martin Seroczyński