SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Mar 2004
    Location
    Grand Junction, CO
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails - Unserializing an attribute

    I want to use a Runt object as an attribute for one of my classes. I can save it to the db fine, looks like...but I need to be able to use it after I pull the object from the db. Right now the attribute is just a String, but I need it to be a Runt::Intersect object. Does anyone know how I can unserialize the attribute so I can use it as an object?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is it serialized in the first place? Does Rails do this automatically?

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Yakima WA.
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah how are you getting it into the db? Are you using serialize :foo for one of your columns in the db? That might be the easiest way. When you use a serialize statement in one of your models the object you want to store in the column gets serialized to yaml before going in and transparently gets dehydrated back into an object when it comes out.

    The main limitation of this method of doing things is that your object needs to be YAML dumpable. This means no procs or lamdas. You could try to create on of your runt objects in irb or something and then dump it to yaml and reload it to see if it survives the translation. Like this:

    Code:
    require 'yaml'
    
    class Foo
      attr_accessor :bar
      attr_accessor :qux
      def initialize(bar, qux)
        @bar, @qux = bar, qux
      end
    end
    
    a = Foo.new("hello", "world")
    p a # => #<Foo:0x219f00 @qux="world", @bar="hello">
    b = YAML.load(YAML.dump a)
    p b # => #<Foo:0x444408 @qux="world", @bar="hello">
    But if you try to add a proc or lamda in the mix it will not work:

    Code:
    require 'yaml'
    
    class Foo
      attr_accessor :bar
      attr_accessor :qux
      def initialize(bar, qux)
        @bar, @qux = bar, lambda { qux.reverse.upcase }
      end
    end
    
    a = Foo.new("hello", "world")
    p a # #<Foo:0x4160bc @qux=#<Proc:0x0041962c@(irb):38>, @bar="hello">
    a.qux.call # => "DLROW"
    b = YAML.load(YAML.dump a)
    TypeError: allocator undefined for Proc
            from /usr/lib/ruby/1.8/yaml.rb:347:in `allocate'
            from /usr/lib/ruby/1.8/yaml.rb:347:in `object_maker'
            from /usr/lib/ruby/1.8/yaml/rubytypes.rb:36
            from /usr/lib/ruby/1.8/yaml/rubytypes.rb:34:in `call'
            from (irb):46:in `transfer'
            from /usr/lib/ruby/1.8/yaml.rb:119:in `load'
            from /usr/lib/ruby/1.8/yaml.rb:119:in `load'
            from (irb):46
    Hope that helps some.

    -Ezra

  4. #4
    SitePoint Addict
    Join Date
    Mar 2004
    Location
    Grand Junction, CO
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't do anything in particular. I just created a bytea column (BLOB for MySQL guys), and did
    myrecord.timex = intersectobject
    myrecord.save

    So I want to some how be able to use myrecord.timex as a Runt::Intersect object (which is what it's saved as) instead of a String. Right now if I do
    myrecord.timex.include? it thinks that timex is a String and is doing the String#include?, which isn't going to work for me. I need to use Runt::Intersect#include?

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to use the Rails serialize helper as mentioned above to serialize/deserialise the object.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Stillwater, MN
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what is getting saved is intersectobject.to_s
    Rad Smith
    My blog

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a note: You don't need to "require 'yaml'" as its built directly into rails.

  8. #8
    SitePoint Addict
    Join Date
    Mar 2004
    Location
    Grand Junction, CO
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have
    serialize :timex, Runt::Intersect

    In my Event model. But it's still coming out as a string.

    Is it even possible to make an object an attribute? I would think so..that's why we have binary field types in the db, right?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •