SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Dec 12, 2005, 09:41 #1
- 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?
-
Dec 12, 2005, 09:45 #2
How is it serialized in the first place? Does Rails do this automatically?
-
Dec 12, 2005, 10:44 #3
- 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">
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
-Ezra
-
Dec 12, 2005, 20:34 #4
- 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?
-
Dec 13, 2005, 02:50 #5
I think you need to use the Rails serialize helper as mentioned above to serialize/deserialise the object.
-
Dec 13, 2005, 17:06 #6
- 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
-
Dec 13, 2005, 22:47 #7
- 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.
-
Dec 14, 2005, 00:44 #8
- 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