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 self.id.to_s 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 + Time.now.year.to_s +
self.id.to_s
self.save
end

private

def get_ccode

Customer.find_by_sql(“select last_insert_id() as code”)[0].code

end

end