如何删除 ActiveRecord 对象?

发布于 2024-10-02 22:23:11 字数 315 浏览 2 评论 0原文

如何删除 ActiveRecord 对象?

我查看了 活动记录查询,它没有任何我可以看到的删除内容。

  1. id删除,

  2. 删除当前对象,如:user.remove,< /p>

  3. 您可以根据 where 子句删除吗?

How do you delete an ActiveRecord object?

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

  1. Delete by id,

  2. Delete the current object like: user.remove,

  3. Can you delete based on a where clause?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

末が日狂欢 2024-10-09 22:23:11

它是 destroydestroy_all 方法,例如

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

您也可以使用 deletedelete_all ,它们不会强制执行 :before_destroy:after_destroy 回调或任何依赖关联选项。

User.delete_all(condition: 'value') 将允许您删除记录
没有主键

注意:根据@hammady的评论,如果用户模型没有主键,user.destroy将不起作用。

注释 2:根据 @pavel-chuchuva 的评论,带条件的 destroy_all 和带条件的 delete_all 已在 Rails 5.1 中弃用 - 请参阅guides.rubyonrails。 org/5_1_release_notes.html

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.

User.delete_all(condition: 'value') will allow you to delete records
without a primary key

Note: from @hammady's comment, user.destroy won't work if User model has no primary key.

Note 2: From @pavel-chuchuva's comment, destroy_all with conditions and delete_all with conditions has been deprecated in Rails 5.1 - see guides.rubyonrails.org/5_1_release_notes.html

碍人泪离人颜 2024-10-09 22:23:11

deletedelete_alldestroydestroy_all

这些文档是:旧文档Rails 3.0.0 docs

delete 不会实例化对象,而 destroy 则这样做。一般来说,deletedestroy 更快。

There is delete, delete_all, destroy, and destroy_all.

The docs are: older docs and Rails 3.0.0 docs

delete doesn't instantiate the objects, while destroy does. In general, delete is faster than destroy.

画▽骨i 2024-10-09 22:23:11
  1. User.destroy

User.destroy(1) 将删除具有 id == 1:before_destroy 和 < code>:after_destroy 回调发生。例如,如果您有关联的记录,

has_many :addresses, :dependent => :destroy

则在用户被销毁后,他的地址也将被销毁。
如果您改用删除操作,则不会发生回调。

  1. User.destroyUser.delete

  2. < p>User.destroy_all()User.delete_all()

注意:User 是一个类,user 是一个实例对象

  1. User.destroy

User.destroy(1) will delete user with id == 1 and :before_destroy and :after_destroy callbacks occur. For example if you have associated records

has_many :addresses, :dependent => :destroy

After user is destroyed his addresses will be destroyed too.
If you use delete action instead, callbacks will not occur.

  1. User.destroy, User.delete

  2. User.destroy_all(<conditions>) or User.delete_all(<conditions>)

Notice: User is a class and user is an instance object

多孤肩上扛 2024-10-09 22:23:11

如果您使用 Rails 5 及更高版本,以下解决方案将起作用。

#delete based on id
user_id = 50
User.find(id: user_id).delete_all

#delete based on condition
threshold_age = 20
User.where(age: threshold_age).delete_all

https://www.rubydoc.info/docs/rails/ActiveRecord%2FNullRelation:删除_全部

If you are using Rails 5 and above, the following solution will work.

#delete based on id
user_id = 50
User.find(id: user_id).delete_all

#delete based on condition
threshold_age = 20
User.where(age: threshold_age).delete_all

https://www.rubydoc.info/docs/rails/ActiveRecord%2FNullRelation:delete_all

猥琐帝 2024-10-09 22:23:11

从特定表中删除一行或从表中删除记录集非常简单,您所需要做的就是从控制台中通过其 ID 获取记录集,然后删除或销毁它。

Deleting a row from a particular table or a record set from a table is pretty simple, from your console all you need to do is grab the record set by its Id then delete or destroy it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文