Home > Software design >  Rails – I want to change the status of my table by putting a certain time?
Rails – I want to change the status of my table by putting a certain time?

Time:05-25

I've only been in RoR for a short time.

I have a list table with a field: status(enabled, disabled) and end_date, I have my form where I put a certain end time. I would like that when reaching this end time, the status field changes to disabled. is there anything i can do to achieve this ?

create_table "lists", force: :cascade do |t|
t.integer "user_id"
t.binary "uuid", limit: 36
t.string "name"
t.string "description"
t.datetime "end_date"
t.integer "status", default: 1
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_lists_on_user_id"

end

CodePudding user response:

You can create a method that checks if the end time is the same as current date and then set the status value

def self.update_status
  List.where(end_date: DateTime.current).update_all(status: 'disabled')
end

Now you can call this method from the scheduler to run this method every min, every hour, or every day as per your requirement. Checkout whenever gem which is popular to write scheduled jobs.

every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot 
  runner "List.update_status"
end
  • Related