Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshal or Serialize an ActiveRecord object

I'm using rails 3.2 and Ruby 1.9.3

I have a need to store data in Redis for caching purposes. The data element I want to save is a hash of different types of elements -- some of which are currently ActiveRecord objects.

I want to store this compound object in redis (an in-memory key/value store). To do so I need to serialize / deserialize the compound object in a way that brings it back together correctly.

How can I serialize or marshal this hash so it comes back together into the same set of objects when I pull it out of cache?

like image 475
Kevin Bedell Avatar asked May 09 '12 15:05

Kevin Bedell


Video Answer


1 Answers

Try using Marshal.dump(obj) and Marshal.load(marshal_str). Here are the DOCS

like image 64
Iuri G. Avatar answered Sep 25 '22 23:09

Iuri G.