ruby - Generating token is returning nil in rails -


hi trying generate token authenticate rails api client. have used generate_key_method in mode class.

class apikey < activerecord::base  before_create :generate_access_token  validates :access_token, uniqueness: true  def generate_access_token   begin     self.access_token = securerandom.hex   end while self.class.exists?(access_token: access_token)  end end 

inserts null while creating record

before_create callbacks happen after validation. in case, uniqueness validation failing , halting callback chain before before_create callbacks can triggered. can set access token before validating on create:

before_validation :generate_access_token, on: :create 

please see page on active record callbacks more information , whole ordering.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -