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?

回答:

1.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.

 

2.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!

http://stackoverflow.com/questions/1297111/check-if-record-was-just-destroyed-in-rails

 

你可能感兴趣的:(Rails,Go)