Ruby-on-rails 从数据库中删除记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5360864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:42:37  来源:igfitidea点击:

Deleting a record from database

ruby-on-railsrubyruby-on-rails-3activerecord

提问by Justin Meltzer

In Ruby on Rails, is it @variable.deleteor @variable.destroy

在 Ruby on Rails 中,是@variable.delete还是@variable.destroy

回答by lebreeze

@variable.destroywill call all callbacks (before_destroy etc.) as well as ensure associations are respected. @variable.deletejust calls the raw database query to delete the object. You're generally much safer off using destroyeven if it's more expensive.

@variable.destroy将调用所有回调(before_destroy 等)并确保尊重关联。@variable.delete只是调用原始数据库查询来删除对象。destroy即使它更昂贵,您通常也更安全。

回答by Adam Lassek

Assuming that you're using ActiveRecord, and that @variableis an instance of an ActiveRecord::Basesubclass, you call @variable.destroy.

假设您使用的是 ActiveRecord,并且它@variable是一个ActiveRecord::Base子类的实例,则调用@variable.destroy.