Rails 2.3.5
I have a Folder model and a Contact Model. In a view, I have a form to delete folders with a radio button to choose between deleting all Contacts in the folder or moving those contacts to a different folder (via a select).
In the respond_to block, if the user has selected to move the Contacts to a new folder, I have this code:
Contact.update_all({:folder_id => params[:folder_to_move_contacts_to]},['folder_id = ?', "#{params[:folder_to_delete]}"])
folder = Folder.find(params[:folder_to_delete])
folder.destroy
flash[:notice] = "SUCCESS: Folder deleted and all contacts moved to new folder."
format.html { redirect_to 'new' }
format.js
It works but I’m wondering how to handle a failure, like if the db hiccups (if the update, which moves the contacts, fails – don’t delete the folder). Is there a way to detect if the update was successful before performing the destroy?
Actually (still being fairly new), I’ve also always wondered why in Scaffold a destroy method seems to just assume the destroy is successful.
Thanks – much appreciated
Active record will raise some
ActiveRecordErrorif the db fails to perform the update, so the destroy won’t be performed.