Skip to main content

Ruby on Rails: How to load session objects into console

This took a little bit of poking around, so I decided to post it here.

Basically I was tracking down a bug, which resulted in a corrupt object (cart) in the web session. So I thought, it would be nice to play with this object in the console to see what's up... But how do I get it in there?

There may be a more elegant way to do this but here is how I got it to load:
  1. Look in tmp/sessions and find the most recent file (ls -alrt on UNIX). Let's say the file is called 'tmp/sessions/ruby_sess.8eb9614a7e4e1e3b'
  2. Open console and type:
    >> session = Marshal.load(File.open('tmp/sessions/ruby_sess.8eb9614a7e4e1e3b'))
    => {"hash"=>{:cart=...
    >> cart = session["hash"][:cart]
    ....
    
    In this case I was trying to access a cart object in the session, which was placed in the session with:session[:cart] = Cart.new
That's it!

Comments

Anonymous said…
What if you store the session in the DB?
Anonymous said…
AR Session Store:

Find the session id from the cookie and then Session.find_by_id(:my_session_id)
LemmingJoel said…
CGI::Session::ActiveRecordStore::Session.find_by_session_id(session_id)

Popular posts from this blog

Car or Auto Make-Model-Year Database : For Breakfast

Make Model What?If you like me were tasked with loading a database of recent car makes/models/years, you would start by looking on the web and seeing if someone else just has it out there, readily available, hopefully for free, but perhaps for a tiny nominal fee.?If only it was that simple... I looked and looked, and couldn't find anything that would fit the above requirements. So I thought, who would know about US car models better than Kelly Blue Book? So I went on their site, and sure enough they have a javascript file that lists all known to them makes and models of used cars. Since the file is public, I figured it's not really "evil" if I scrape and parse it for my own benefit. Disagree? Have a better source? Then leave a comment.Anyway, to cut the long story short, I'm hoping to save a day or so to someone else who may, like me, be looking for this information. The ruby module shown below retrieves and parses the javascript from KBB site into a Ruby da…

Why I Like PostgreSQL

Today I gave a short presentation at work about PostgreSQL, and why I much prefer it to MySQL.

PostgreSQL vs MySQL: Eternal Battle
I may be misreading this, but it seems that there is a recent trend within startups to move away from MySQL, probably thanks to folks like Heroku on one side (who use PostgreSQL to the extreme, and help and contribute to it's development), vs folks like Oracle on the other side, tainting the "open source pureness" of MySQL :)

At my work we currently use a mid-sized MySQL 5.1 Percona instance, which is holding up quite well I must admit. Both PostgreSQL and MySQL have definitely converged to cover most features that people want, but my leaning is still towards PostgreSQL. I just agree with it's focus on data integrity, recovery, constraints, extensibility, while some of the early decisions in MySQL's design do not agree with me at all (like truncating long strings, 1/0 instead of booleans, ambiguous group by, etc). I think that data …

Nagios checks for Sidekiq Queue size monitoring and Joyent RAM size

At Wanelo we are committed to sharing as much of our code as possible with the world, since we are ourselves highly reliant on open source movement (ruby, rails, etc).

Here is a small but growing project with simple BASH shell scripts used by Nagios monitoring system to ensure things are running smoothly.

https://github.com/wanelo/nagios-checks

Please see the examples in the README on how to use it with nagios.