selva4210-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2012-Aug-24 10:46 UTC
ActiveRecord storing arrays and hashes
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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 tobe 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/U9S5Hu5M6DUJ. For more options, visit https://groups.google.com/groups/opt_out.
selva4210-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2012-Aug-24 11:12 UTC
Re: Re: ActiveRecord storing arrays and hashes
On Fri, Aug 24, 2012 at 4:35 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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.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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/oCBNHClNTLkJ. For more options, visit https://groups.google.com/groups/opt_out.
Reasonably Related Threads
- Fwd: Read a Number column with leading zeros using Roo gem
- serialized attribute converting to string on reload
- Setting up omniauth with salesforce login in rails 2.3.8( ruby 1.8.6)
- Best library for reading/wriiting excel in ruby
- [HELP] - Ruby Drag and Drop script on screen element in Mac OS