如何在活动记录中删除?如何在

2023-09-08 15:48:04 作者:染辞

如何在活动记录中删除?

我看着活动记录查询,它不会对删除,我可以看到任何东西。

删除按id,

删除像现在的对象: user.remove

可以删除基于where子句?

解决方案

这是破坏 destroy_all 方法,如

  user.destroy
User.find(15).destroy
User.destroy(15)
User.where(年龄:20).destroy_all
User.destroy_all(年龄:20)
 

另外,您可以使用删除 DELETE_ALL 将不执行:before_destroy :after_destroy 回调或任何相关的关联选项

How do you delete in active record?

清除网页中的浏览记录

I looked at Active Record Querying and it does not have anything on deleting that I can see.

Delete by id,

Delete the current object like: user.remove,

Can you delete based on a where clause?

解决方案

It's destroy and destroy_all methods, like

user.destroy
User.find(15).destroy
User.destroy(15)
User.where(age: 20).destroy_all
User.destroy_all(age: 20)

Alternatively you can use delete and delete_all which won't enforce :before_destroy and :after_destroy callbacks or any dependent association options.

 
精彩推荐
图片推荐