I am trying to generate a "Client Number" automatically when an admin generates a new client in our Rails 4 app.
I have read several articles on this but most are talking about breakable tokens.
so far my Client model only has a client_number attribute and it is a string.
my model looks like:
class Client < ActiveRecord::Base
self.client_number = SecureRandom.hex(2).upcase
NameError: undefined local variable or method `generate_client_number' for #<Class:0x007fc59bdd7008>
Did you mean? generated_attribute_methods
Client Exists (0.2ms) SELECT 1 AS one FROM "clients" WHERE "clients"."client_number" IS NULL LIMIT 1
=> #<Client id: nil, client_number: nil, created_at: nil, updated_at: nil>
It's just a matter of using a symbol correctly:
These after_* and before_* hooks are defined by a symbol with the name of the method called, so you need to write :generate_client_number, with :, instead of just generate_client_number.
About the fact it is not generating a number, your error message implies your are getting a duplicate and having a roolback. I suggest you to try
self.client_number = SecureRandom.uuid.upcase