rails: serialize :column, Hash returns string
We upgraded our app from Rails 4.1 to Rails 4.2 recently. We have following column in the DB on one of the models:
t.text "simulation_outputs"
In the model:
serialize :simulation_outputs, Hash
Sometimes when model is loaded hash is returned as string:
> foo.simulation_outputs
=> "{3000.0=>0.068}”
Is there something we should’ve done during upgrade? Is this a known issue?
There is nothing weird with how hash is constructed: it is created manually by assigning keys to a hash and then saved to a model.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 17 (7 by maintainers)
@a17levine This happened to me when I was updating our app from 4.1 to 4.2. I’m glad I saw your comment and by just moving the
serialize
method on top of the model file, it worked magically 😂I just had this problem upgrading a 4.1 app to 4.2. It was only happening with one of my models but happening consistently in that model so I wrote a test.
What fixed the problem was moving the
serialize
call closer to the top of the model file. It was originally way down.4.2 must be implementing
serialize
in a way that is affected by load order because the same test passed with Rails 4.1 but failed in 4.2