In my rails application, I have a work model in which I want the status to be automatically changed to “archived” 30 days after approval by the administrator, is this possible? if so, what is the way to do this?
I would add an attribute named "archive_time" as the date-time when it enters the approved state. You can then configure the rake task to set the archived state and where archive_time is in the past. It might look like this:
jobs = Job.where("state = ? and archive_time >= ?", 'approved', Time.now) jobs.each {|job| job.archive }
Then plan the rake task to be run once a day. I would use cron for this.
(, ) , . , .
, approved_at, datetime. , approved_at = Time.now.
approved_at
datetime
approved_at = Time.now
, , :
def is_archived? approved_at && approved_at < Time.now - 30.days end
, :
scope :archived, -> { where('approved_at IS NOT NULL AND approved_at <?', Time.now - 30.days)}