I have a situation where I have a model in ruby that has been deleted in the database. The object still exists in memory. I want a way to check to see if the object in the db still exists.
If I debug the code, and stop on a breakpoint, I can see the object in the database through a sql query and through irb. However, in the debugger, I can evaluate expressions and have tried:
car.reload
Car.find(car.id)
Car.where(id: car.id)
All of which return a car that does not exist.
I am also trying to use ActiveModel::Dirty and tried this in the debugger:
car.changed?
This returns false.
TLDNR: All signs in the debugger point to it existing. The Db says it does not exist.
I have exhausted the option that the debugger is in a bad state. I am 1000% confident that the issue is with memory objects vs db objects.
How can I get the real state of the object from the code?
ActiveRecord::RecordNotFoundif the record no longer exists. I've verified for myself that this is indeed the case (at least in ActiveRecord 4.0). So I'm not 1000% confident that the record really doesn't exist in your db. I guess we should start with which version of ActiveRecord are you using?#reload. Since that's not working for you, I can only suggest that you re-check your evidence that it's not in the db.#reloador::find. If that's not working, then something else is preventing the behavior you expect, not just that you don't know the right api.