Hi,
When trying to store a Array or Hash to a string field, ActiveRecord automatically serializes them to yaml. If that's so why is there a special option called serialize?
Hi,
When trying to store a Array or Hash to a string field, ActiveRecord automatically serializes them to yaml. If that's so why is there a special option called serialize?
The automatic serialization is just a side effect that everything has to be forced into a string before active record will have it. Because Active Record doesn’t know that the column is a serialzied one, when you try and retrieve the value you’ll just get the raw yaml string. Using serialise tells Active Record to unserialize when reading.
Fred
Oops yeah. I was checking this within a rails console without reloading it. After reloading, the attribute just returns a yaml string.
Thanks for the clarification!
It also exists so we can build custom serializers, for example I have several fields like role which are symbolized and string-ified using a serializer and a custom serializer to store an array of tags as a NULL hstore hash.