I have a field token_number in my database which i want to assign 1 more than the previous record.
for example in terms of array
token_number[2] = token_number[1] + 1
And the value would be 1 for the first record. How can a assign a variable in the model which changes everytime i create a new record from my controller.
And my next condition is that the variable be reset to 0 every time when the current time i.e Time.now is the midnight. I tried it like this
class Ticket < ApplicationRecord
before_create :update_token_number
private
def update_token_number
date = Time.now
time_zone = Time.zone # any time zone really
timeday = time_zone.local(date.year, date.month, date.day)
midday = timeday.beginning_of_day
if date == midday
num = 0
num = num + 1
else
num = num + 1
end
self.token_num = num
#end
end
end
if it runs also the value given to the field is always 1.
Please help