Hi everyone,
I've got an app that's going well, but I've encountered a little setback. For the most part, it's a simple maneuver. I want to be able to delete a directory when an entry is deleted. When someone manually deletes it, I've got this code in the controller:
Code Ruby:def delete @user = User.find(session[:user_id]) @todelete = Cpost.find(params[:id]) @todelete.destroy @cpostdir = "#{RAILS_ROOT}/public/images/users/#{@user.screen_name}/#{@todelete.id}" FileUtils.remove_dir(@cpostdir) if File.exists?(@cpostdir) redirect_to :controller => 'cposts', :action => 'list' flash[:notice] = "Your entry has been deleted." end
That's all well and good, it works exactly how you suspect it would. Now, what I want to do is automatically delete entries after a certain amount of time, let's say, when they're 15 days old.
This is what I've got in my app controller so far (a before filter calls the method)
Code Ruby:def remove_old_cposts Cpost.destroy_all(["created_at < ?", 15.days.ago]) end
It removes all entries older than 15 days. The thing I need added is to remove the directory that corresponds to each one of those entries being deleted. If you noticed in the method I posted before, the specific directory contains the screen name of the person who posted it, and finally the ID of the post (both directories in an arbitrarity root/images/users/ dir.) So, in an entry made by the user "Tester1", the images would be placed in root/images/users/Tester1/:id/-images-go-here. And Tester2's would be in root/images/users/Tester2/:id/-images-go-here. Finally, Tester3's are in root/images/users/Tester3/:id/-images-go-here. Let's say Tester1 and Tester3's are older than 15 days, how would I go about finding the directories to those? It's easy when you're deleting one single file, since you have the Cpost ID and the session user, but not when you're destroying a bunch of stuff at once.
What's the best way to also remove those directories automatically?
Thanks in advance!






Bookmarks