王梦琪的博客

记录技术学习及感悟

作业:destroy_all delete_all 区别

| Comments

具体内容见:http://stackoverflow.com/questions/6698207/delete-all-vs-destroy-all

“If you want to delete the User and all associated objects -> destroy_all However, if you just want to delete the User without suppressing all associated objects -> delete_all

According to this post : Rails :dependent => :destroy VS :dependent => :delete_all

destroy / destroy_all: The associated objects are destroyed alongside this object by calling their destroy method
delete / delete_all: All associated objects are destroyed immediately without calling their :destroy method

Destroy_all彻底删除,delete_all是比destroy_all更加高效,delete_all是一条SQL DELETE声明,直接用于数据库,不会实例化记录,不会调用destroy方法,不会引用回调。

u = User.find_by_name('Jq')
u.usage_indexes.destroy_all
u.user_stats.destroy_all
u.delete
但如果把destroy_all换成delete_all,只会删掉用户自己表单中的ID,其他表单中这个用户的记录不会被删掉,只是在ID栏位变为空。

Comments

comments powered by Disqus