Hi all, I want to serialize an object and store it into the database. This seems a trivial task but Google gives me no useful hints. Can someone please give me a little help on what methods I can use to serialize and what type of column I best use to store serialized objects. Many thanks Stijn --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 10 Apr 2008, at 16:15, Tarscher wrote:> > Hi all, > > I want to serialize an object and store it into the database. This > seems a trivial task but Google gives me no useful hints. > > Can someone please give me a little help on what methods I can use to > serialize and what type of column I best use to store serialized > objects.If you do class Foo < ActiveRecord::Base serialize :bar end then the bar attribute will be serialized/deserialized as needed Fred> Many thanks > Stijn > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the answer. I can get the serialized object out of the database but it is now in YAML format. How can I convert this back into my vlass object? Thanks On Apr 10, 5:49 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10 Apr 2008, at 16:15,Tarscherwrote: > > > > > Hi all, > > > I want to serialize an object and store it into the database. This > > seems a trivial task but Google gives me no useful hints. > > > Can someone please give me a little help on what methods I can use to > > serialize and what type of column I best use to store serialized > > objects. > > If you do > class Foo < ActiveRecord::Base > serialize :bar > end > > then the bar attribute will be serialized/deserialized as needed > > Fred > > > Many thanks > > Stijn--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tarscher wrote:> Thanks for the answer. > > I can get the serialized object out of the database but it is now in > YAML format. How can I convert this back into my vlass object? > > Thanks > > On Apr 10, 5:49�pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>Look at the Marshal module. From the API docs: class Klass def initialize(str) @str = str end def sayHello @str end end (produces no output) o = Klass.new("hello\n") data = Marshal.dump(o) obj = Marshal.load(data) obj.sayHello #=> "hello\n" -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rails usually does the translation for you. All you need to do is access that attribute. If you saved it as an Array, then you should treat the attribute like any other array. The other way is to save it as a Hash and then you will treat it as a Hash when you access it. On Sep 18, 8:28 am, Guy Van den berg <rails-mailing-l...@andreas- s.net> wrote:> Tarscher wrote: > > Thanks for the answer. > > > I can get the serialized object out of the database but it is now in > > YAML format. How can I convert this back into my vlass object? > > > Thanks > > > On Apr 10, 5:49 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > Look at the Marshal module. From the API docs: > > class Klass > def initialize(str) > @str = str > end > def sayHello > @str > end > end > > (produces no output) > > o = Klass.new("hello\n") > data = Marshal.dump(o) > obj = Marshal.load(data) > obj.sayHello #=> "hello\n" > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---