Henley Wing Chiu
Henley Wing Chiu

Reputation: 22535

Postgres/Rails => is object guaranteed to be persisted if I do .save?

If I have a model and save it like this:

model = Website.new
model.attr = 1
model.id = 1
model.save #assume no errors in saving

then retrieve it like this:

model2 = Website.find(1)

Will model2 always be returned? Ignoring errors saving to the database.

Is there a possible scenario where the data is not yet committed to the database, and as a result the find results in no records found? Do I need to delay the find to guarantee the row is returned?

Upvotes: 0

Views: 55

Answers (1)

CDub
CDub

Reputation: 13354

Assuming no database errors, and assuming you haven't overwritten save on Website, the only race condition you'd have is if you try to access the object (via find or otherwise) in the milliseconds before the record is created in the database.

So, to directly answer your question - yes, it's possible - but given a single database (e.g. no read-only slaves or anything like that), it's highly, highly unlikely.

Upvotes: 1

Related Questions