Forum: Ruby on Rails ActiveRecord storing arrays and hashes

Posted by selva4210@gmail.com (Guest)
on 2012-08-27 13:59
(Received via mailing list)
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?

--
Azhagu Selvan

http://tamizhgeek.in
Posted by Frederick Cheung (Guest)
on 2012-08-27 14:01
(Received via mailing list)
On Friday, August 24, 2012 11:46:35 AM UTC+1, Azhaguselvan SP wrote:
>
> 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
Posted by selva4210@gmail.com (Guest)
on 2012-08-27 14:05
(Received via mailing list)
On Fri, Aug 24, 2012 at 4:35 PM, Frederick Cheung
<frederick.cheung@gmail.com> wrote:
>>
> 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.

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!



--
Azhagu Selvan

http://tamizhgeek.in
Posted by Jordon Bedwell (Guest)
on 2012-08-27 14:15
(Received via mailing list)
On Friday, 24 August 2012 06:12:02 UTC-5, Azhaguselvan SP wrote:

>
> Oops yeah. I was checking this within a rails console without reloading
> it.
> After reloading, the attribute just returns a yaml string.
>

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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.