So I have a video.rb model, that allows users to upload a video.
The videos are being uploaded to YouTube. In order to save the YT link to the record, I first need to create the object, handle the upload via the YT API, then update the record with the YT link.
This works fine, except for errors/issues with the upload. If, for whatever reason, the video doesn’t get successfully uploaded to YT and the user abandons the upload process, the unnecessary video record is left in the DB.
So, ideally, what I would love to do is after something (either some time or some event) I check to see if the record had the YT link successfully added to it (i.e. if the video record actually became a “legit” video record with an actual video).
If it didn’t become a “legit video”, then I want to clean up the DB and remove that dummy record.
What’s the best way to approach this?
This is the process in my app:
Video Created (VideosController#Create)
Video Upload (VideosController#Upload)
Video Save (VideosController#Save) - this then updates the attributes for the video with the YT URL.
I would probably go with a super simple MVP approach to this and just make a rake task that is scheduled via the Heroku Scheduler to run everyday at like 11pm or something.
Something like:
# Assumes you are storing the URL as a string
Video.all.map { |v| v.destroy unless v.url != "" }
Idk I would keep it super basic and just do something like that. Maybe create a class if you wanted to take it one step further. Also if you are doing TDD you definitely want a class or some way to test it so a rake task won’t work. You could do:
:task => :environment do
cleaner = VideoCleaner.new
cleaner.begin
end
class VideoCleaner
def initialize ; end
def begin
Video.all.map { |v| v.destroy unless v.url != "" }
end
end
Those method names could use some work, but that’s the basic gist of how I’d handle that. Super simple and basic is always my preference.
You could try and handle it the same way the discourse/facebook/twitter handles abandoning a post.If the user navigates away from the page, confirm they don’t intend to upload etc and trigger the deletion on a case by case basis that way?