<ActiveRecord::Relation 删除全部(一条记录/实例) production 和development 日志的区分

删除全部 ()

ActiveRecord::Relation

删除记录而不首先实例化记录,因此不调用 #destroy 方法或调用回调。 这是一条直接进入数据库的 SQL DELETE 语句,比 destroy_all. 但要小心关系,特别是 :dependent不遵守在关联上定义的规则。 返回受影响的行数。


Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all

两个调用都使用一条 DELETE 语句一次性删除所有受影响的帖子。 如果您需要销毁依赖关联或致电您的 before_*或者 after_destroy回调,使用 destroy_all代替方法。

如果提供了无效的方法, delete_all引发 ActiveRecordError:


Post.distinct.delete_all
# => ActiveRecord::ActiveRecordError: delete_all doesn't support distinct

delete_all() Link

Deletes the records 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. Returns the number of rows affected.

Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all

Both calls delete the affected posts all at once with a single DELETE statement. If you need to destroy dependent associations or call your before_* or after_destroy callbacks, use the destroy_all method instead.

If an invalid method is supplied, delete_all raises an ActiveRecordError:

Post.distinct.delete_all
# => ActiveRecord::ActiveRecordError: delete_all doesn't support distinct

Source: show | on GitHub

 

 How To Use IRB to Explore Ruby | DigitalOceanhttps://www.digitalocean.com/community/tutorials/how-to-use-irb-to-explore-rubyirb 的使用

是通用的ruby环境

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值