Find current Id

Hi, Can someine tell me how to find the latest/current id from the database?

Thanks, fries 88

I thought you already got this answered?

Model.find(:first, :order => “id DESC”).id

This is subject to race conditions. If you get the latest id, and then use it, it is possible that by the time you use it, some other user has inserted a new row and the highest id is now different. This is an inherent property of any good RDBMS.

If your controller does a '' call, and it succeeds, the id is immediately stored in the record as '', but I prefer the form 'modelrecord[:id]', as the first form is confusable with the deprecated Ruby method that is now available as 'whatever.object_id'. The id that save gives you is right for your new object, but, because of the race condition, is not guaranteed to be the latest and greatest in the table.


The whole concept of 'greatest'/'latest' id is subject to race conditions. Why do you need to the latest id in the db ? Fred