Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if record was just destroyed in rails

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?

like image 420
Daniel Huckstep Avatar asked Aug 18 '09 23:08

Daniel Huckstep


2 Answers

Just do it:

record.destroyed? 

Details are here ActiveRecord::Persistence

like image 198
Voldy Avatar answered Sep 28 '22 03:09

Voldy


You can do this.

Record.exists?(record.id) 

However that will do a hit on the database which isn't always 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.

like image 28
ryanb Avatar answered Sep 28 '22 05:09

ryanb