Rails Sessions

Posted by John
on Sunday, 09 March 2008

rails is love

To make a truly intelligent web application your gonna sooner or later have to play around with Sessions, which is basically a Cookie's big brother.

Store in DB

Now Rails initally will store all it's session data in a text file within the /tmp/sessions directory of your rails app, which is usually ok, but if you're gonna build a production-ready app you're gonna want to up the ante somewhat and store them in your DB.

This is simply done by opening up your environment.rb file and un-commenting the following line...

(within /config/environment.rb) config.actioncontroller.sessionstore = :activerecordstore

Then in the command-line generate a RAKE DB migration for your Session objects (so they're stored in your db from now on).

rake db:sessions:create rake db:migrate

Bingo, you now have a table within your database hooked up and ready to store your session data perfectly.

Storing Session Objects

Now that you've got your DB storing your session data, why don't we start creating some session objects.

This is done by...

session[:order] = 'ASC'

Here, we've simply created a new session object, storing in it a text value of 'ASC'; cool eh?

We can then check to see if our session is empty via...

if session[:order].blank? ..do stuff..

So if our session object contains nothing we can initialise it correctly.

Common Gotcha

Now one of the good things with switching sessions over to your db is that it'll secure your app a little bit better and your performance will improve.

Plus if your building your app on your dev machine, then transfer it to your live box, but can't find a reason why your sessions don't work there it's usually down to file permissions for the session file; by switching sessions over to a db you remove any future problem of this.

Good eh,

Rake Migrations and Custom Tasks

Posted by John
on Sunday, 16 December 2007

Jesse James

One cool thing about Rails is it provides you with so many helper functions and commands, one of these being RAKE.

RAKE is similar to the MAKE compiler command used to build binaries from source, with Rails it's useful when building your apps database.

Database Work

Create Migration

script/generate migration users

This will create a Rails database migration you can use to specify the contents of the users table for instance.

/myapp/db/migrate/001_create_users.rb

Create Database

Once that's defined you can create the database with,

rake db:create

Compile Pending Migrations

Then run any pending database migrations.

rake db:migrate

All of which will build into a schema located at,

/myapp/db/schema.rb

Custom Tasks

You can also write custom RAKE tasks to do other things,

What to put in them

Here's a simple rake task,

task :sayHello do
  puts "Hello World"
end

Where to save your Scripts

Simple, in the tasks directory,

/myapp/lib/tasks

e.g.

/myapp/lib/tasks/sayhello.rake

And to Run ?

rake sayHello

Returns...

Hello World

More Info at...