[Rails] How to organize monthly billing?

I need every month to bill the user, which formed the basis of the parameters that he set. So far, except for the crown did not occur. But in my opinion is a crutch, needs to be a better option. If someone faced with this challenge, how are you organized?
PS. Write on ruby with rails, although in this case it probably doesn't matter.
September 26th 19 at 07:06
4 answers
September 26th 19 at 07:08
Once a day at 00:01 run cron which will generate invoices for all users.
Everything else is from the evil one :)

You can of course when a user logs in to your personal account first, verify the account if the account is not - create.
select `id` from `bill` where `user_id`=123 and `time_create`>now() - interval 1 month and `payd`='n' limit 1;

if id > 0 score was found, display it to the user and highlight that there is unpaid bill.
if id = 0 no account generate.
but if it is necessary to consider the time of day ? - nona.Baumbach39 commented on September 26th 19 at 07:11
well, then you can work days and not intervals
to_days(`time_create`) > (to_days(now()) - 10) and hour(now()) > 18

the account is set if the last billing has been more than 10 days and current time greater than 18 hours - Hardy_Luettgen48 commented on September 26th 19 at 07:14
September 26th 19 at 07:10
before_action :update_last_seen

def update_last_seen
 if current_user
 if (Time.now - 3.hours) >= current_user.last_seen
 current_user.last_seen = Time.now
current_user.save
end
end


So I have one project checked and updated when the last time a person was on
the website, the accuracy was not important, so + 3 hours Time.now - 3.hours
However you can change on the day etc. But this only works if the person visits the site.
Hang this before_action in ApplicationController and when loading any page, will run this method.
September 26th 19 at 07:12
I think Kron is a good option. Use gem https://github.com/javan/whenever

And the schedule just specify

every :month do 
 runner "User.set_bill" # method of billing users according to your settings
end
September 26th 19 at 07:14
mperham.github.io/sidekiq
https://github.com/tobiassvn/sidetiq

class MyWorker
 include Sidekiq::Worker
 include Sidetiq::Schedulable

 recurrence { monthly }

 def perform
 # do stuff ...
end
end

Find more questions by tags Ruby on Rails