Reputation: 2900
In my app user can be blocked if failed the Legal Test - which means that the user model will change its status from active to inactive user.status = 'inactive'
. The business requirement is to return the status to active after 3 days.
I know I could use cron job like below:
every 1.day, :at => '12:00 am' do
User.where(status_updated_at: Date.today <= 3.days.ago)
.update_all(status: 'active')
end
But instead of searching the whole User db maybe there is another, better way to achieve this?
Upvotes: 0
Views: 170
Reputation: 36860
You don't really need to schedule a job, you can do this when you need to (i.e. the user actually tries to sign in) with an after_initialize
class User
after_initialize :reinstate_if_ready
def reinstate_if_ready
self.status = 'active' if status_updated_at.nil? || status_updated_at <= 3.days.ago)
end
end
Upvotes: 1