Compruebe si el registro se acaba de destruir en rieles

83

Entonces alli esta

record.new_record?

Para comprobar si algo es nuevo

Necesito comprobar si algo está saliendo.

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?

Daniel Huckstep
fuente

Respuestas:

51

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.

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

And then check record.destroyed.

ryanb
fuente
2
I think some context is helpful here. It depends on why you want to know if an object has been destroyed. If you're doing it as part of a unit test, then the extra hit against the database is fine. If you're doing it as part of the app, then going the second route might be a better bet.
jerhinesmith
67
This is no longer necessary, use object.destroyed?
Rob
2
in Rails 4, DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to exists?. Please pass the id of the object by calling .id.
Minqi Pan
5
Still need Record.exists? in controller specs, because I can't reload destroyed record to check if it is destroyed?. But if I have instance variable to check with assings, then I can use destroyed?.
denis.peplin
@Daniel Huckstep, if you are still alive, please, change the accepted answer for the correct one!
Daniel
11

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

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.

So there you go. Coming soon!

Steve Klabnik
fuente
6

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.

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 ;)

DGG
fuente
5

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::RecordNotFound if you did something like 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.

theIV
fuente
I currently just check frozen?, but really I can load something and freeze it for some other purpose, and it would be a lie then... In the current situation, frozen? works fine, but I don't want to rely on it long term.
Daniel Huckstep
0

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.

Mike Buckbee
fuente