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?
ruby-on-rails
ruby
activerecord
Daniel Huckstep
fuente
fuente
object.destroyed?
DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to
exists?. Please pass the id of the object by calling
.id.
Record.exists?
in controller specs, because I can't reload destroyed record to check if it isdestroyed?
. But if I have instance variable to check withassings
, then I can usedestroyed?
.Just do it:
Details are here ActiveRecord::Persistence
fuente
This is coming very soon. In the latest Riding Rails post, it says this:
So there you go. Coming soon!
fuente
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 ;)
fuente
destroy
ing an object doesn't return anything other than a call tofreeze
(as far as I know) so I thinkfrozen?
is your best bet. Your other option is to rescue fromActiveRecord::RecordNotFound
if you did something likerecord.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.
fuente
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.
fuente