Jeremy (Auzigog)
2008-Jul-18 19:33 UTC
Serialize not working as expected. Am I doing it wrong?
I reported the bug here: http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/647-serialize-d-array-not-unserializing-properly But the replies are to the tone of "it''s not a bug". Serialize isn''t working how I expect it to, but I was wondering if I''m just looking at it all wrong. The code I''m using is essentially what you see in the bug report. Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeremy (Auzigog)
2008-Jul-18 19:43 UTC
Serialize not working as expected. Am I doing it wrong?
I reported the bug here: http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/647-serialize-d-array-not-unserializing-properly But the replies are to the tone of "it''s not a bug". Serialize isn''t working how I expect it to, but I was wondering if I''m just looking at it all wrong. The code I''m using is essentially what you see in the bug report. Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The serialized Array is getting unserialized correctly. However, I think YAML needs to know about the Peg class before you can unserialize if you want the elements of the array to be Peg instances. Like someone at the rails bug tracker, I think you need to require and/or include peg.rb. One solution is to have the Peg class defined in board.rb before the Board definition (or maybe after). This worked for me: #board.rb class Peg def meth "foo" end end class Board < ActiveRecord::Base serialize :pegs end Loading development environment (Rails 2.1.0)>> b = Board.new=> #<Board id: nil, pegs: nil, created_at: nil, updated_at: nil>>> b.pegs=> nil>> b.pegs = []=> []>> b.pegs<< Peg.new=> [#<Peg:0xb7683adc>]>> b.save=> true>> b = Board.find 1=> #<Board id: 1, pegs: [#<Peg:0xb766ec90>], created_at: "2008-07-18 19:58:01", updated_at: "2008-07-18 19:58:01">>> b.pegs.first.meth=> "foo">> quitOn Jul 18, 3:33 pm, "Jeremy (Auzigog)" <auzi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I reported the bug here:http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/64... > > But the replies are to the tone of "it''s not a bug". Serialize isn''t > working how I expect it to, but I was wondering if I''m just looking at > it all wrong. > > The code I''m using is essentially what you see in the bug report. > > Thanks.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeremy (Auzigog)
2008-Jul-18 20:09 UTC
Re: Serialize not working as expected. Am I doing it wrong?
I mentioned in my original post on the bug tracker that you either need to include or require peg.rb (or write the class inside board.rb like you did). My point is that it doesn''t seem like the expected behavior. It seems like rails should automatically include/require the class for you, just like it always would. I''m not sure if that responsibility should be on YAML or on ActiveRecord, but it seems that it should be done automagically by one of the classes involved. On Jul 18, 12:59 pm, julian <thefool...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The serialized Array is getting unserialized correctly. > > However, I think YAML needs to know about the Peg class before you can > unserialize if you want the elements of the array to be Peg > instances. Like someone at the rails bug tracker, I think you need to > require and/or include peg.rb. One solution is to have the Peg class > defined in board.rb before the Board definition (or maybe after). > This worked for me: > > #board.rb > class Peg > def meth > "foo" > end > end > > class Board < ActiveRecord::Base > serialize :pegs > end > > Loading development environment (Rails 2.1.0)>> b = Board.new > > => #<Board id: nil, pegs: nil, created_at: nil, updated_at: nil>>> b.pegs > => nil > >> b.pegs = [] > => [] > >> b.pegs<< Peg.new > > => [#<Peg:0xb7683adc>]>> b.save > => true > >> b = Board.find 1 > > => #<Board id: 1, pegs: [#<Peg:0xb766ec90>], created_at: "2008-07-18 > 19:58:01", updated_at: "2008-07-18 19:58:01"> > > >> b.pegs.first.meth > => "foo" > >> quit > > On Jul 18, 3:33 pm, "Jeremy (Auzigog)" <auzi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I reported the bug here:http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/64... > > > But the replies are to the tone of "it''s not a bug". Serialize isn''t > > working how I expect it to, but I was wondering if I''m just looking at > > it all wrong. > > > The code I''m using is essentially what you see in the bug report. > > > Thanks.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-18 21:40 UTC
Re: Serialize not working as expected. Am I doing it wrong?
On Jul 18, 9:09 pm, "Jeremy (Auzigog)" <auzi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I mentioned in my original post on the bug tracker that you either > need to include or require peg.rb (or write the class inside board.rb > like you did). > > My point is that it doesn''t seem like the expected behavior. > > It seems like rails should automatically include/require the class for > you, just like it always would. I''m not sure if that responsibility > should be on YAML or on ActiveRecord, but it seems that it should be > done automagically by one of the classes involved.I don''t think it can be ActiveRecord: it doesn''t understand yaml at all, it just gets a blob of data which it hands off to yaml. Yaml being a pure ruby thing doesn''t do the magic autoloading stuff that rails does and so you''re stuck. 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-/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 -~----------~----~----~----~------~----~------~--~---
Jeremy (Auzigog)
2008-Jul-18 23:52 UTC
Re: Serialize not working as expected. Am I doing it wrong?
That''s the thing, YAML *does* do all sorts of autoloading. All it has it text, and it turns that into the proper classes. I do think you have a point about the separation, Frederick. I would make the assumption that YAML has a list of folders it looks in when loading classes. If that were publicly accessible (IE - Rails could access it), then all rails would have to do is add the app/ models/* folders and app/lib/* folders to that list of autoloading locations. But I''m probably making too many assumptions there. On Jul 18, 2:40 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 18, 9:09 pm, "Jeremy (Auzigog)" <auzi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I mentioned in my original post on the bug tracker that you either > > need to include or require peg.rb (or write the class inside board.rb > > like you did). > > > My point is that it doesn''t seem like the expected behavior. > > > It seems like rails should automatically include/require the class for > > you, just like it always would. I''m not sure if that responsibility > > should be on YAML or on ActiveRecord, but it seems that it should be > > done automagically by one of the classes involved. > > I don''t think it can be ActiveRecord: it doesn''t understand yaml at > all, it just gets a blob of data which it hands off to yaml. Yaml > being a pure ruby thing doesn''t do the magic autoloading stuff that > rails does and so you''re stuck. > > 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-/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 -~----------~----~----~----~------~----~------~--~---
I find it unexpected behavior that I read your ticket and didn''t read the solution you posted right before posting the exact same solution :- P Anyways, you might want to check this out if you haven''t already: http://itsignals.cascadia.com.au/?p=10 On Jul 18, 7:52 pm, "Jeremy (Auzigog)" <auzi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That''s the thing, YAML *does* do all sorts of autoloading. All it has > it text, and it turns that into the proper classes. > > I do think you have a point about the separation, Frederick. > > I would make the assumption that YAML has a list of folders it looks > in when loading classes. If that were publicly accessible (IE - Rails > could access it), then all rails would have to do is add the app/ > models/* folders and app/lib/* folders to that list of autoloading > locations. > > But I''m probably making too many assumptions there. > > On Jul 18, 2:40 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Jul 18, 9:09 pm, "Jeremy (Auzigog)" <auzi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I mentioned in my original post on the bug tracker that you either > > > need to include or require peg.rb (or write the class inside board.rb > > > like you did). > > > > My point is that it doesn''t seem like the expected behavior. > > > > It seems like rails should automatically include/require the class for > > > you, just like it always would. I''m not sure if that responsibility > > > should be on YAML or on ActiveRecord, but it seems that it should be > > > done automagically by one of the classes involved. > > > I don''t think it can be ActiveRecord: it doesn''t understand yaml at > > all, it just gets a blob of data which it hands off to yaml. Yaml > > being a pure ruby thing doesn''t do the magic autoloading stuff that > > rails does and so you''re stuck. > > > 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-/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 -~----------~----~----~----~------~----~------~--~---