I have a rails class that serializes one attribute. class Statistic < ActiveRecord::Base serialize :userlist end When a statistic object is loaded and it''s userlist changed from a String to an Array userlist always gets serialized back into a String. Objects that are properly deserialized into Arrays stay that way. The framework seems to remember and deserialize :userlist into a String even if it went in as an Array. >> s = Statistic.find 238 => #<Statistic id: 238, userlist: "--- \n- 2222437\n- \"99779\"\n- \"120429\"\n- \"210503\"\n- 32..."> # Note here: :userlist is an Array in YAML. Why doesn''t it get correctly deserialized? >> s.userlist.class => String >> s.userlist = s.userlist.split(/\s+/) >> s.userlist.class => Array >> s.save => true >> s.reload => #<Statistic id: 238,userlist: "--- \n- 2222437\n- \"99779\"\n- \"120429\"\n- \"210503\"\n- 32..."> >> s.userlist.class => String The goal of this exercise is to convert all String userlists to Array. If I change the class (serialize :userlist, Array) before converting I get TypeMismatch exceptions. ActiveRecord::SerializationTypeMismatch: userlist was supposed to be a Array, but was a String Is there a way to force AR to interpret userlist as an Array? % rails --version Rails 2.3.4 -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Related, but doesn''t solve the problem: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/e548784faa7f9cef/c24380c98c64cbc3 On Feb 26, 9:03 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a rails class that serializes one attribute. > > class Statistic < ActiveRecord::Base > serialize :userlist > end > > When a statistic object is loaded and it''s userlist changed from a > String to an Array userlist always gets serialized back into a String. > Objects that are properly deserialized into Arrays stay that way. The > framework seems to remember and deserialize :userlist into a String > even if it went in as an Array. > > >> s = Statistic.find 238 > => #<Statistic id: 238, userlist: "--- \n- 2222437\n- \"99779\"\n- > \"120429\"\n- \"210503\"\n- 32..."> > # Note here: :userlist is an Array in YAML. Why doesn''t it get > correctly deserialized? > > >> s.userlist.class > => String > >> s.userlist = s.userlist.split(/\s+/) > > >> s.userlist.class > => Array > > >> s.save > => true > > >> s.reload > => #<Statistic id: 238,userlist: "--- \n- 2222437\n- \"99779\"\n- > \"120429\"\n- \"210503\"\n- 32..."> > > >> s.userlist.class > => String > > The goal of this exercise is to convert all String userlists to Array. > If I change the class (serialize :userlist, Array) before converting I > get TypeMismatch exceptions. > > ActiveRecord::SerializationTypeMismatch: userlist was supposed to > be a Array, but was a String > > Is there a way to force AR to interpret userlist as an Array? > > % rails --version > Rails 2.3.4-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Found the problem. The String is not correct YAML:>> YAML::load(s.userlist)ArgumentError: syntax error:ScannerException while scanning a quoted scalar we had this found unexpected end of stream from (irb):8 The code from AR::B is def object_from_yaml(string) return string unless string.is_a?(String) && string =~ /^---/ YAML::load(string) rescue string end --Dean On Feb 26, 9:03 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a rails class that serializes one attribute. > > class Statistic < ActiveRecord::Base > serialize :userlist > end > > When a statistic object is loaded and it''s userlist changed from a > String to an Array userlist always gets serialized back into a String. > Objects that are properly deserialized into Arrays stay that way. The > framework seems to remember and deserialize :userlist into a String > even if it went in as an Array. > > >> s = Statistic.find 238 > => #<Statistic id: 238, userlist: "--- \n- 2222437\n- \"99779\"\n- > \"120429\"\n- \"210503\"\n- 32..."> > # Note here: :userlist is an Array in YAML. Why doesn''t it get > correctly deserialized? > > >> s.userlist.class > => String > >> s.userlist = s.userlist.split(/\s+/) > > >> s.userlist.class > => Array > > >> s.save > => true > > >> s.reload > => #<Statistic id: 238,userlist: "--- \n- 2222437\n- \"99779\"\n- > \"120429\"\n- \"210503\"\n- 32..."> > > >> s.userlist.class > => String > > The goal of this exercise is to convert all String userlists to Array. > If I change the class (serialize :userlist, Array) before converting I > get TypeMismatch exceptions. > > ActiveRecord::SerializationTypeMismatch: userlist was supposed to > be a Array, but was a String > > Is there a way to force AR to interpret userlist as an Array? > > % rails --version > Rails 2.3.4-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This doesn''t explain why the userlist isn''t correctly serialized & deserialized when reloaded.... --Dean On Feb 26, 9:29 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Found the problem. The String is not correct YAML: > > >> YAML::load(s.userlist) > > ArgumentError: syntax error:ScannerException while scanning a quoted > scalar we had this found unexpected end of stream from (irb):8 > > The code from AR::B is > > def object_from_yaml(string) > return string unless string.is_a?(String) && string =~ /^---/ > YAML::load(string) rescue string > end > > --Dean > > On Feb 26, 9:03 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a rails class that serializes one attribute. > > > class Statistic < ActiveRecord::Base > > serialize :userlist > > end > > > When a statistic object is loaded and it''s userlist changed from a > > String to an Array userlist always gets serialized back into a String. > > Objects that are properly deserialized into Arrays stay that way. The > > framework seems to remember and deserialize :userlist into a String > > even if it went in as an Array. > > > >> s = Statistic.find 238 > > => #<Statistic id: 238, userlist: "--- \n- 2222437\n- \"99779\"\n- > > \"120429\"\n- \"210503\"\n- 32..."> > > # Note here: :userlist is an Array in YAML. Why doesn''t it get > > correctly deserialized? > > > >> s.userlist.class > > => String > > >> s.userlist = s.userlist.split(/\s+/) > > > >> s.userlist.class > > => Array > > > >> s.save > > => true > > > >> s.reload > > => #<Statistic id: 238,userlist: "--- \n- 2222437\n- \"99779\"\n- > > \"120429\"\n- \"210503\"\n- 32..."> > > > >> s.userlist.class > > => String > > > The goal of this exercise is to convert all String userlists to Array. > > If I change the class (serialize :userlist, Array) before converting I > > get TypeMismatch exceptions. > > > ActiveRecord::SerializationTypeMismatch: userlist was supposed to > > be a Array, but was a String > > > Is there a way to force AR to interpret userlist as an Array? > > > % rails --version > > Rails 2.3.4 > >-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Feb-26 17:38 UTC
Re: Rails Seralized My Object One Way & Refuses to Change
On Feb 26, 4:15 pm, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This doesn''t explain why the userlist isn''t correctly serialized & > deserialized when reloaded....What''s the column type & how long is the data ? Is it getting truncated because of the size of the column ? Fred> --Dean > > On Feb 26, 9:29 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Found the problem. The String is not correct YAML: > > > >> YAML::load(s.userlist) > > > ArgumentError: syntax error:ScannerException while scanning a quoted > > scalar we had this found unexpected end of stream from (irb):8 > > > The code from AR::B is > > > def object_from_yaml(string) > > return string unless string.is_a?(String) && string =~ /^---/ > > YAML::load(string) rescue string > > end > > > --Dean > > > On Feb 26, 9:03 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I have a rails class that serializes one attribute. > > > > class Statistic < ActiveRecord::Base > > > serialize :userlist > > > end > > > > When a statistic object is loaded and it''s userlist changed from a > > > String to an Array userlist always gets serialized back into a String. > > > Objects that are properly deserialized into Arrays stay that way. The > > > framework seems to remember and deserialize :userlist into a String > > > even if it went in as an Array. > > > > >> s = Statistic.find 238 > > > => #<Statistic id: 238, userlist: "--- \n- 2222437\n- \"99779\"\n- > > > \"120429\"\n- \"210503\"\n- 32..."> > > > # Note here: :userlist is an Array in YAML. Why doesn''t it get > > > correctly deserialized? > > > > >> s.userlist.class > > > => String > > > >> s.userlist = s.userlist.split(/\s+/) > > > > >> s.userlist.class > > > => Array > > > > >> s.save > > > => true > > > > >> s.reload > > > => #<Statistic id: 238,userlist: "--- \n- 2222437\n- \"99779\"\n- > > > \"120429\"\n- \"210503\"\n- 32..."> > > > > >> s.userlist.class > > > => String > > > > The goal of this exercise is to convert all String userlists to Array. > > > If I change the class (serialize :userlist, Array) before converting I > > > get TypeMismatch exceptions. > > > > ActiveRecord::SerializationTypeMismatch: userlist was supposed to > > > be a Array, but was a String > > > > Is there a way to force AR to interpret userlist as an Array? > > > > % rails --version > > > Rails 2.3.4-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This occurred to me too. The column type is text. I don''t recall the length, but it was between 50,000 & 100,000 characters. I''ll check the max_allowed_packet setting when I get back to work. http://dev.mysql.com/doc/refman/5.0/en/blob.html --Dean On Feb 26, 11:38 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 26, 4:15 pm, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > This doesn''t explain why the userlist isn''t correctly serialized & > > deserialized when reloaded.... > > What''s the column type & how long is the data ? Is it getting > truncated because of the size of the column ? > > Fred > > > --Dean > > > On Feb 26, 9:29 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Found the problem. The String is not correct YAML: > > > > >> YAML::load(s.userlist) > > > > ArgumentError: syntax error:ScannerException while scanning a quoted > > > scalar we had this found unexpected end of stream from (irb):8 > > > > The code from AR::B is > > > > def object_from_yaml(string) > > > return string unless string.is_a?(String) && string =~ /^---/ > > > YAML::load(string) rescue string > > > end > > > > --Dean > > > > On Feb 26, 9:03 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I have a rails class that serializes one attribute. > > > > > class Statistic < ActiveRecord::Base > > > > serialize :userlist > > > > end > > > > > When a statistic object is loaded and it''s userlist changed from a > > > > String to an Array userlist always gets serialized back into a String. > > > > Objects that are properly deserialized into Arrays stay that way. The > > > > framework seems to remember and deserialize :userlist into a String > > > > even if it went in as an Array. > > > > > >> s = Statistic.find 238 > > > > => #<Statistic id: 238, userlist: "--- \n- 2222437\n- \"99779\"\n- > > > > \"120429\"\n- \"210503\"\n- 32..."> > > > > # Note here: :userlist is an Array in YAML. Why doesn''t it get > > > > correctly deserialized? > > > > > >> s.userlist.class > > > > => String > > > > >> s.userlist = s.userlist.split(/\s+/) > > > > > >> s.userlist.class > > > > => Array > > > > > >> s.save > > > > => true > > > > > >> s.reload > > > > => #<Statistic id: 238,userlist: "--- \n- 2222437\n- \"99779\"\n- > > > > \"120429\"\n- \"210503\"\n- 32..."> > > > > > >> s.userlist.class > > > > => String > > > > > The goal of this exercise is to convert all String userlists to Array. > > > > If I change the class (serialize :userlist, Array) before converting I > > > > get TypeMismatch exceptions. > > > > > ActiveRecord::SerializationTypeMismatch: userlist was supposed to > > > > be a Array, but was a String > > > > > Is there a way to force AR to interpret userlist as an Array? > > > > > % rails --version > > > > Rails 2.3.4 > >-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Feb-28 17:42 UTC
Re: Rails Seralized My Object One Way & Refuses to Change
On Feb 28, 3:07 pm, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This occurred to me too. The column type is text. I don''t recall the > length, but it was between 50,000 & 100,000 characters. I''ll check > the max_allowed_packet setting when I get back to work. >Text columns have a 65535 limit (mediumtext and longtext columns can take much more) Fred> http://dev.mysql.com/doc/refman/5.0/en/blob.html > > --Dean > > On Feb 26, 11:38 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On Feb 26, 4:15 pm, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > This doesn''t explain why the userlist isn''t correctly serialized & > > > deserialized when reloaded.... > > > What''s the column type & how long is the data ? Is it getting > > truncated because of the size of the column ? > > > Fred > > > > --Dean > > > > On Feb 26, 9:29 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Found the problem. The String is not correct YAML: > > > > > >> YAML::load(s.userlist) > > > > > ArgumentError: syntax error:ScannerException while scanning a quoted > > > > scalar we had this found unexpected end of stream from (irb):8 > > > > > The code from AR::B is > > > > > def object_from_yaml(string) > > > > return string unless string.is_a?(String) && string =~ /^---/ > > > > YAML::load(string) rescue string > > > > end > > > > > --Dean > > > > > On Feb 26, 9:03 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > I have a rails class that serializes one attribute. > > > > > > class Statistic < ActiveRecord::Base > > > > > serialize :userlist > > > > > end > > > > > > When a statistic object is loaded and it''s userlist changed from a > > > > > String to an Array userlist always gets serialized back into a String. > > > > > Objects that are properly deserialized into Arrays stay that way. The > > > > > framework seems to remember and deserialize :userlist into a String > > > > > even if it went in as an Array. > > > > > > >> s = Statistic.find 238 > > > > > => #<Statistic id: 238, userlist: "--- \n- 2222437\n- \"99779\"\n- > > > > > \"120429\"\n- \"210503\"\n- 32..."> > > > > > # Note here: :userlist is an Array in YAML. Why doesn''t it get > > > > > correctly deserialized? > > > > > > >> s.userlist.class > > > > > => String > > > > > >> s.userlist = s.userlist.split(/\s+/) > > > > > > >> s.userlist.class > > > > > => Array > > > > > > >> s.save > > > > > => true > > > > > > >> s.reload > > > > > => #<Statistic id: 238,userlist: "--- \n- 2222437\n- \"99779\"\n- > > > > > \"120429\"\n- \"210503\"\n- 32..."> > > > > > > >> s.userlist.class > > > > > => String > > > > > > The goal of this exercise is to convert all String userlists to Array. > > > > > If I change the class (serialize :userlist, Array) before converting I > > > > > get TypeMismatch exceptions. > > > > > > ActiveRecord::SerializationTypeMismatch: userlist was supposed to > > > > > be a Array, but was a String > > > > > > Is there a way to force AR to interpret userlist as an Array? > > > > > > % rails --version > > > > > Rails 2.3.4-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 28, 11:42 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 28, 3:07 pm, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > This occurred to me too. The column type is text. I don''t recall the > > length, but it was between 50,000 & 100,000 characters. I''ll check > > the max_allowed_packet setting when I get back to work. > > Text columns have a 65535 limit (mediumtext and longtext columns can > take much more) > > FredThanks. That is the problem. 65535 characters on the nose. --Dean> >http://dev.mysql.com/doc/refman/5.0/en/blob.html > > > --Dean > > > On Feb 26, 11:38 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On Feb 26, 4:15 pm, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > This doesn''t explain why the userlist isn''t correctly serialized & > > > > deserialized when reloaded.... > > > > What''s the column type & how long is the data ? Is it getting > > > truncated because of the size of the column ? > > > > Fred > > > > > --Dean > > > > > On Feb 26, 9:29 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Found the problem. The String is not correct YAML: > > > > > > >> YAML::load(s.userlist) > > > > > > ArgumentError: syntax error:ScannerException while scanning a quoted > > > > > scalar we had this found unexpected end of stream from (irb):8 > > > > > > The code from AR::B is > > > > > > def object_from_yaml(string) > > > > > return string unless string.is_a?(String) && string =~ /^---/ > > > > > YAML::load(string) rescue string > > > > > end > > > > > > --Dean > > > > > > On Feb 26, 9:03 am, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I have a rails class that serializes one attribute. > > > > > > > class Statistic < ActiveRecord::Base > > > > > > serialize :userlist > > > > > > end > > > > > > > When a statistic object is loaded and it''s userlist changed from a > > > > > > String to an Array userlist always gets serialized back into a String. > > > > > > Objects that are properly deserialized into Arrays stay that way. The > > > > > > framework seems to remember and deserialize :userlist into a String > > > > > > even if it went in as an Array. > > > > > > > >> s = Statistic.find 238 > > > > > > => #<Statistic id: 238, userlist: "--- \n- 2222437\n- \"99779\"\n- > > > > > > \"120429\"\n- \"210503\"\n- 32..."> > > > > > > # Note here: :userlist is an Array in YAML. Why doesn''t it get > > > > > > correctly deserialized? > > > > > > > >> s.userlist.class > > > > > > => String > > > > > > >> s.userlist = s.userlist.split(/\s+/) > > > > > > > >> s.userlist.class > > > > > > => Array > > > > > > > >> s.save > > > > > > => true > > > > > > > >> s.reload > > > > > > => #<Statistic id: 238,userlist: "--- \n- 2222437\n- \"99779\"\n- > > > > > > \"120429\"\n- \"210503\"\n- 32..."> > > > > > > > >> s.userlist.class > > > > > > => String > > > > > > > The goal of this exercise is to convert all String userlists to Array. > > > > > > If I change the class (serialize :userlist, Array) before converting I > > > > > > get TypeMismatch exceptions. > > > > > > > ActiveRecord::SerializationTypeMismatch: userlist was supposed to > > > > > > be a Array, but was a String > > > > > > > Is there a way to force AR to interpret userlist as an Array? > > > > > > > % rails --version > > > > > > Rails 2.3.4 > >-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.