questions about generating customer_code when creating customer

I am working a MIS which have some customer management work. When the
customer object is creating, we need to generate a unique code for the
customer. When in oracle, I created a sequence to solve the problem.
But now I migrated to mysql, and I tried to use the id created by auto-
increment. But it fails because it have to call save in after_save,
causing a infinite loop. what I want is a unique 6 digit number to
append to the code,which I use in my error code.
thx in advance for your reply

class Customer < ActiveRecord::Base
  CTYPES = [
    ['Person', 'P'],
    ['Corporation', 'C'],
    ['Annual Corp', 'A']
  belongs_to :nation
  attr_accessor :code
  def after_save()
    nation = Nation.find(nation_id)
    self.code = nation.nation_code + ctype + +