views:

69

answers:

1

Long introduction:

Normally all data necessary for my web application are stored in session variables. Only if the user explicitly says that something is to be saved, it is stored into the database. (Like saving a comment or creating an order from a shopping cart)

In my latest Rails application I noticed that I really could use many of Rails' ActiveRecord helpers like find for example. Unfortunately some functions like find need a stored record.

I also have a hierarchical structure of temporary ActiveRecords, but they don't have IDs and I really could use those IDs to identify individual records between controller actions.

Short question:

How can I write these temporary records into the database and thus gain all of the ActiveRecords functionality, but make sure those records are cleared as soon as the session becomes invalid?

+2  A: 

YES, use the session ID as A PK, and a expire date/time column and write the info into a table or tables. if the session ID is large use an identity/auto number as the PK and just store and index the session ID, use the identity/auto number as the FK to other tables if necessary.

Also, you'll need some sort or garbage collection job to run every x minutes to delete everything with an expire date greater than the current date/time.

KM
Don't forget to update the expire date on every page load.
AndyMcKenna
@AndyMcKenna good point, but since you'd be updating all the data too, I kind of assumed that. Depending on the complexity of the session info to store, I'd consider deleting it every time and then just inserting it fresh each page load too.
KM