ruby on rails - ActiveRecord serialization problem -


i have db column serialized hash:

class foo < activerecord::base    serialize :bar end 

when store hash inside of bar few levels deep, deeper levels not seem deserialize when need them. objects 1 level deep deserialzed fine. however, objects 2 or more levels deep remain yaml classes.

i tried manually deserializing using yaml::load() got error saying argument wasn't instance of io.

does know why complete ruby object doesn't deserialize?

edit: after further investigation, problem seems stem fact i'm calling virtual attribute serialized yaml.

class foo < activerecord::base    serialize :bar end  class bar < activerecord::base     attr_accessor :enabled end  @bars = @foo.bar[:bars] @bars.each |bar|    puts bar.enabled end 

yields:

nomethoderror: undefined method `enabled' #<yaml::object:0xb6f11844>     (irb):12     (irb):11:in `each'     (irb):11     :0 

does mean deserialization isn't "real", ie, yaml object acts similar original object, it's not actual instance of object?

there's 2 things keep in mind when serializing user-defined objects instead of "plain old ruby" objects.

  • you need have user-defined classes loaded before object can de-serialized or instances of generic yaml::object type. de-serializing not auto-load classes.
  • some objects may not serialize without customization, though not case.

generally serializing instances of activerecord::base object bad idea these have extraordinary amount of baggage need encoded in addition attribute. best build class derived directly object, default behavior, or simple base class of sort.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -