Hi, Can someine tell me how to find the latest/current id from the database? Thanks, fries 88 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I thought you already got this answered? Model.find(:first, :order => "id DESC").id On Jan 11, 2008 5:02 PM, fries 88 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > Can someine tell me how to find the latest/current id from the database? > > Thanks, > fries 88 > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ''modelrecord.save'' call, and it succeeds, the id is immediately stored in the record as ''modelrecord.id'', 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. F --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11 Jan 2008, at 07:47, fredistic wrote:> > 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 ''modelrecord.save'' call, and it succeeds, > the id is immediately stored in the record as ''modelrecord.id'', 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---