views:

145

answers:

2

Is there a compact way with ActiveRecord to query for what id it's going to use next if an object was going to be persisted to the database? In SQL a query like this would look something like:

SELECT max(id) + 1 FROM some_table;
+1  A: 

Think this should work:

YourModel.last.id + 1
fig
A: 

Hi Randombits,

While accepting fig-gnuton's answer I might want to draw your attention to a small thing. If you are getting the next ID to set to a particular record before saving, I think its not a good idea.

because as an example in a web based system 1 - you get the last id as 10 2 - you set the next id as 11 3 - before you save the record someone else has saved the record, now the last id should be 12 likewise..

I'm not sure you want the last id to do what I'm thinking here, But if so this is just to draw your attension

cheers,

sameera

sameera207