如何在Ruby on Rails中使ActiveRecord显示下一个ID(上一个+ 1)? [英] How do I get ActiveRecord to show the next id (last + 1) in Ruby on Rails?
本文介绍了如何在Ruby on Rails中使ActiveRecord显示下一个ID(上一个+ 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 的版本:
Model.maximum(:id).next
这篇关于如何在Ruby on Rails中使ActiveRecord显示下一个ID(上一个+ 1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文