Ruby on Rails delete 和 destroy 的区别

来源:互联网 发布:求实软件操作流程 编辑:程序博客网 时间:2024/04/28 05:21

Basically "delete" sends a query directly to the database to delete the record. In that case Rails doesn't know what attributes are in the record it is deleteing nor if there are any callbacks (such as before_destroy).

The "destroy" method takes the passed id, fetches the model from the database using the "find" method, then calls destroy on that. This means the callbacks are triggered.

You would want to use "delete" if you don't want the callbacks to be triggered or you want better performance. Otherwise (and most of the time) you will want to use "destroy".

Delete: 在数据库中直接删除记录

Destroy:先在model中找到该记录,然后在删除,这意味着会调用callback,比如”before destroy“


转载自:

http://archive.railsforum.com/viewtopic.php?id=2427


0 0