A Ruby on Rails Journey

February 26, 2008

Datestamp the Cart

Filed under: Agile Web Development with Rails — Dick @ 1:36 am

Since the cart is just an order with a flag set and it lives in the database, I’ll need some way to clear out old orders. Visitors will create carts and abandon them and they’d live there forever if I don’t do some maintenance. I don’t know how or when I’ll do that maintenance, but I know I’ll need a date in the cart to determine how old it is. I start by adding a field to the table. In a new migration called 008_add_last_access_date.rb:

class AddLastAccessDate < ActiveRecord::Migration
  def self.up
    add_column :o rders, :last_access, :datetime
  end

  def self.down
    remove_column :o rders, :last_access
  end
end

Every time I change the cart, I need to update this field. In order.rb, I created a new private method

 def update_last_access
   self.last_access = DateTime.now
   self.save
 end

And I called that in both the add_product and remove_product methods. I made the method private, but I’m not sure I needed to.

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.