views:

90

answers:

1

I find it confusing that there is an ActiveRecord delete as well as destroy.

In my Controller I want to delete a user in my delete action.

The result would be that this instance represented by the User model no longer exists in the database.

Which method should I use for this?

+4  A: 

delete is faster as it bypasses callbacks, but by that same reason it's less functional; destroy is therefore a safer way to go, unless you positively know the callbacks are unnecessary in a specific case.

Alex Martelli
When you say "callbacks" do you mean some other methods that the database calls after Rails requests the database to delete a record? How would you know what these callbacks do and whether you need them or not?
eggdrop
The callbacks are before_destroy and after_destroy (they're Ruby methods, what do you mean by "the database calls"?).
Alex Martelli
I wasn't sure what you meant by "callbacks" - whether you meant methods that are called by Rails or methods that are called by Oracle/MySql/etc. But I think I understand now that before_destroy and after_destroy are Rails callbacks which, perhaps I could define if I wanted to perform some logic before or after an object is deleted, such as writing to a log file?
eggdrop
Yep, you can define the two callbacks as needed -- the point is, they're bypassed by `delete`, which essentially maps to a SQL DELETE (if you have triggers in your DB schema which that DELETE will fire, then those will fire anyway when the DELETE happens).
Alex Martelli
Ok, so I can think of delete as a SQL DELETE while destroy performs some additional before and after housecleaning duties. Thanks.
eggdrop
Yep, a fair and simple summary (there's "freezing" the object too, but that's in both cases, so, doesn't distinguish them).
Alex Martelli