... how can I do so that the user can choose the deadline
year, month, day, hours and minutes
Use select_datetime
, which will provide fields for year, month, day, hour, minutes and seconds.
... when the time and date of the system is the same as deadline
, the
field finished
change to true
, how do you get this in rails?
The simplest way is to use some gem for scheduling tasks (eg Delayed Job , Sidekiq , Resque , among others), with which you program a task every time you generate a new competition record.
For example, you could create a method in your model (for the example I call it Competicion
) that is responsible for updating the field finished
and send the notification email 1 :
class Competicion < ApplicationRecord
# ...
def fin_competicion
self.update(:finished, true)
CompeticionMailer.fin(self).deliver_now
end
end
And you would schedule the task from your controller using Delayed Job 2 :
class CompeticionController < ApplicationController
# ...
def create
@competicion = Competicion.new(competicion_params)
if @competicion.save
@competicion.delay(run_at: @competicion.deadline).fin_competicion
# código para creación exitosa
else
# código para creación no exitosa
end
end
# ...
end
1 It is assumed that the mailer CompeticionMailer
was created with the fin
method.
2 It is assumed that Delayed Job has already been installed and configured.