rails删除数据

简单讲,一句话,delete不会触发 回调
效率比较高


Deletes the row with a primary key matching the id argument, using a SQL DELETE statement, and returns the number of rows deleted. Active Record objects are not instantiated, so the object‘s callbacks are not executed, including any :dependent association options or Observer methods.

You can delete multiple rows at once by passing an Array of ids.

Note: Although it is often much faster than the alternative, destroy, skipping callbacks might bypass business logic in your application that ensures referential integrity or performs other essential jobs.

Ruby代码   收藏代码spinner.gif
  1. # Delete a single row  
  2. Todo.delete(1)  
  3.   
  4. # Delete multiple rows  
  5. Todo.delete([2,3,4])  
  # Delete a single row  Todo.delete(1)   # Delete multiple rows  Todo.delete([2,3,4])


Deletes the records matching conditions without instantiating the records first, and hence not calling the destroy method nor invoking callbacks. This is a single SQL DELETE statement that goes straight to the database, much more efficient than destroy_all. Be careful with relations though, in particular :dependent rules defined on associations are not honored.
Ruby代码   收藏代码spinner.gif
  1. Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')")  
  2. Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something''Else'])  
  Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')")  Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else'])


Destroy an object (or multiple objects) that has the given id, the object is instantiated first, therefore all callbacks and filters are fired off before the object is deleted. This method is less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run.

This essentially finds the object (or multiple objects) with the given id, creates a new object from the attributes, and then calls destroy on it.
Ruby代码   收藏代码spinner.gif
  1. # Destroy a single object  
  2. Todo.destroy(1)  
  3.   
  4. # Destroy multiple objects  
  5. todos = [1,2,3]  
  6. Todo.destroy(todos)  
  # Destroy a single object  Todo.destroy(1)   # Destroy multiple objects  todos = [1,2,3]  Todo.destroy(todos)


Destroys the records matching conditions by instantiating each record and calling its destroy method. Each object‘s callbacks are executed (including :dependent association options and before_destroy/after_destroy Observer methods). Returns the collection of objects that were destroyed; each will be frozen, to reflect that no changes should be made (since they can‘t be persisted).

Note: Instantiation, callback execution, and deletion of each record can be time consuming when you‘re removing many records at once. It generates at least one SQL DELETE query per record (or possibly more, to enforce your callbacks). If you want to delete many rows quickly, without concern for their associations or callbacks, use delete_all instead.
Ruby代码   收藏代码spinner.gif
  1. Person.destroy_all("last_login < '2004-04-04'")  
  2. Person.destroy_all(:status => "inactive")  
  Person.destroy_all("last_login < '2004-04-04'")  Person.destroy_all(:status => "inactive")


posted on 2011-03-15 13:45  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2011/03/15/1984806.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值