如何让 ActiveRecord 在 Ruby on Rails 中显示下一个 id (last + 1)? [英] How do I get ActiveRecord to show the next id (last + 1) in Ruby on Rails?

查看:13
本文介绍了如何让 ActiveRecord 在 Ruby on Rails 中显示下一个 id (last + 1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个对象将被持久化到数据库中,ActiveRecord 是否有一种紧凑的方式来查询它接下来将使用的 id?在 SQL 中,这样的查询类似于:

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;

推荐答案

这里略有修改 Taryn East'版本:

Here is slightly modified Taryn East's version:

Model.maximum(:id).next

这篇关于如何让 ActiveRecord 在 Ruby on Rails 中显示下一个 id (last + 1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆