21 Jan 2010

Rails dependent delete_all gotcha

I was working on a Rails application the other day I made a small mistake that I thought I would just post about briefly to give a fair warning.

We all know that we can delete/destroy dependencies within ActiveRecord by utilizing the :dependent option on the relationship.



class Whatever < ActiveRecord::Base

  has_many :things, :dependent => delete_all 
 
end 
 

If you look twice, you see I forgot to make "delete_all" a symbol. So when this class got loaded, I of course received an error. So without thinking anything of this, I fixed my syntax error, but then noticed all my records were gone. Well, after thinking about it for a few, I figured since delete_all was not a symbol, what actually got evaluated was "Whatever.delete_all", thus deleting all my records.

Anyone else ever do this on accident before? :-)