Thursday, November 17, 2011

What the hell! Can't serialize a Mysql object with Marshal in Ruby

Okey so got this strange information late in night at about 3.50am and it took more than 2 hrs for me to surrender. uff :D
and this is something that came in picture
`dump': no marshal_dump is defined for class Mysql (TypeError

and this is a small code snippet which will give you this situation
require "rubygems"
require "mysql"

class RandomClass
def initialize
@db_instance = Mysql.real_connect("hostname", "username", "password", "database_name")
end
end

random_class = RandomClass.new
a = Marshal.dump(random_class)

and there you are, so one can't serialize a object with MySql object in it and with the set of finding i can even say that serializing even with yaml is also not possible.

So work around to this that i can suggest is to do a close of live mysql object in the method itself so that at any give time when object will be passed to serialization using Marshal.dump there would be no live MySql object to hinder with serialization :)

try this piece of snippet now

require "rubygems"
require "mysql"

class RandomClass
def initialize
db_instance = Mysql.real_connect("hostname", "username", "password", "database_name")
db_instance.close
end
end

random_class = RandomClass.new
a = Marshal.dump(random_class)


And this should work :)

Honda Civic & A/C problems.

Hello Friends, Got a post again on to Honda Civic (The good old favorite commuter of mine). This car has been doing great except for so...