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
rders, :last_access, :datetime
end
def self.down
remove_column
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.