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' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -