Titan
2008-Oct-14 00:58 UTC
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---