You are looking for the table's AUTO_INCREMENT value. MySQL stores such metadata in the INFORMATION_SCHEMA database. The AUTO_INCREMENT values can be found in the TABLES table. The table contains one entry for each database and table.
I don't think Rails or the MySQL gem provide any built-in method for fetching it.
I have used something like this in one of my previous projects:
# config/initializers/auto_increment.rb
module AutoIncrement
def auto_increment_value
connection.execute(<<-SQL.squish).first[0]
SELECT `AUTO_INCREMENT`
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE `TABLE_SCHEMA` = '#{connection.current_database}'
AND `TABLE_NAME` = '#{table_name}'
SQL
end
end
ActiveRecord::Base.extend(AutoIncrement)
You can then execute:
MyModel.auto_increment_value
and it will return the current value for the table's AUTO_INCREMENT value, e.g. 11
.
Note that it is not safe to use that value as an explicit ID for your record. You should let MySQL handle the assignment of new IDs – that's what AUTO_INCREMENT is for.