Forum: Ruby on Rails questions about generating customer_code when creating customer

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Titan (Guest)
on 2008-10-14 04:59
(Received via mailing list)
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 + +
#  private
#  def get_ccode
#    Customer.find_by_sql("select last_insert_id() as code")[0].code
#  end
This topic is locked and can not be replied to.