Blog » Blog Entry

Get the next MySQL auto_increment id using ActiveRecord

May 22, 2008

I see this come up on the Rails list from time to time, so I figured I'd document it here as a blog entry.

To get the next auto_increment id from a MySQL table using ActiveRecord:

@next = ActiveRecord::Base.connection.execute( "
  SELECT auto_increment
  FROM information_schema.tables
  WHERE table_name='foo'
" ).fetch_hash['auto_increment'].to_i
Tags: rubyonrails, mysql

« VirtualBox "no workie" after kernel upgrade Simple git setup for new git users »

Add a comment:

Title:

Comment:

Name:

Email: