Ruby-on-rails 检查记录是否刚刚在 rails 中被销毁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1297111/
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-02 21:38:21  来源:igfitidea点击:

Check if record was just destroyed in rails

ruby-on-railsrubyactiverecord

提问by Daniel Huckstep

So there is

所以有

record.new_record?

To check if something is new

检查东西是否是新的

I need to check if something is on it's way out.

我需要检查一下是否有什么东西出来了。

record = some_magic
record.destroy
record.is_destroyed? # => true

Something like that. I know destroying freezes the object, so frozen? sort of works, but is there something explicitly for this task?

类似的东西。我知道销毁会冻结对象,所以冻结?有点工作,但有什么明确的任务吗?

采纳答案by ryanb

You can do this.

你可以这样做。

Record.exists?(record)

However that will do a hit on the database which isn't really necessary. The only other solution I know is to do a callback as theIV mentioned.

但是,这会对数据库造成影响,这并不是真正必要的。我知道的唯一其他解决方案是按照 theIV 提到的方式进行回调。

attr_accessor :destroyed
after_destroy :mark_as_destroyed
def mark_as_destroyed
  self.destroyed = true
end

And then check record.destroyed.

然后检查record.destroyed

回答by Voldy

Just do it:

去做就对了:

record.destroyed?

Details are here ActiveRecord::Persistence

详细信息在这里ActiveRecord::Persistence

回答by Steve Klabnik

This is coming very soon. In the latest Riding Railspost, it says this:

这很快就会到来。在最新的Riding Rails帖子中,它说:

And finally, it's not necessarily BugMash-related, but José Valim - among dozens of other commits - added model.destroyed?. This nifty method will return true only if the instance you're currently looking at has been successfully destroyed.

最后,它不一定与 BugMash 相关,但 José Valim - 在其他几十个提交中 - 添加了 model.destroyed?。仅当您当前正在查看的实例已成功销毁时,这个漂亮的方法才会返回 true。

So there you go. Coming soon!

所以你去。即将推出!

回答by DGG

While record.destroyed? works fine, and does return true or false, you can also DRY this up a little bit and create the if condition on the line you call destroy on in your controller.

虽然记录被破坏了?工作正常,并且确实返回 true 或 false,您也可以稍微干燥一下并在您在控制器中调用 destroy on 的行上创建 if 条件。

record = Object.find(params[:id])
if record.destroy
  ... happy path
else
  ... sad path
end

Realize this post is a bit late in the game. But should anyone want to discuss this more, i'm game! Side note: I also had an after_destroy validation on my model and while it worked, a separate method for something like this seems like overkill ;)

意识到这篇文章在游戏中有点晚了。但是如果有人想更多地讨论这个,我就是游戏!旁注:我也对我的模型进行了 after_destroy 验证,虽然它有效,但对于此类事情的单独方法似乎有点矫枉过正;)

回答by theIV

destroying an object doesn't return anything other than a call to freeze(as far as I know) so I think frozen?is your best bet. Your other option is to rescue from ActiveRecord::RecordNotFoundif you did something like record.reload.

destroying 一个对象除了调用freeze(据我所知)之外不会返回任何东西,所以我认为frozen?是你最好的选择。ActiveRecord::RecordNotFound如果你做了类似的事情,你的另一个选择是拯救record.reload

I think Mike's tactic above could be best, or you could write a wrapper for these cases mentioned if you want to start 'making assumptions'.

我认为上面迈克的策略可能是最好的,或者如果你想开始“做出假设”,你可以为提到的这些案例编写一个包装器。

Cheers.

干杯。

回答by Mike Buckbee

Without knowing more of the logic of your app, I think that frozen? is your best bet.

在不了解您的应用程序逻辑的情况下,我认为冻结了?是你最好的选择。

Failing that, you could certainly add a "destroyed" attribute to your models that you trigger in the callbacks and that could be checked against if you want a more precise solution.

如果做不到这一点,您当然可以向在回调中触发的模型添加“销毁”属性,如果您想要更精确的解决方案,可以对其进行检查。