Reputation: 119
is there a way to automatically delete posts/products/articles or anything created on a rails 6 app? I'm trying to build an online image repository that users can upload images that appear only for 24 hours and then get deleted.I have the posting and saving of the pictures/text through active storage and Postgres but i can't get it to get deleted automatically. I've read about whenever sidekiq and resque scheduler but i find it difficult to understand and make it work. i tried from some tutorials and reading the documentation but I'm still having trouble. Can anyone point me in the right direction or try to help me?
Upvotes: 0
Views: 467
Reputation: 1771
You have many options
The simplest one is creating a rake task and setting up a cron job to call this time every minute or something like that. If you call it every 24h you may end up with posts staying for up to 47h.
You can use delayed jobs in two ways
2.1 In an after_create callback, set a job to delete the post after 24 hours. Something like this handle_asynchronously :in_the_future, run_at: Proc.new { 24.hours.from_now }
2.2 Using delayed_job_recurring gem to do the same thing in option one, but without the need of using cron
Edit: I would use option 2.1 since it's the simplest one and easier to maintain, the only downside is that it will create a job for every post, but that shouldn't be a problem even with a million posts a day
Upvotes: 3