Ruby on Rails的:如何获得的ActiveRecord,显示下一个ID(最后+ 1)? [英] Ruby on Rails: How to get ActiveRecord to show the next id (last + 1)?

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

问题描述

有没有用ActiveRecord一个紧凑的方式来查询什么ID它会用接下来的一个对象是否将要被保存到数据库?在这样的SQL查询看起来是这样的:

  SELECT MAX(ID)+ 1 some_table;
 

解决方案

认为这应该工作:

  YourModel.last.id + 1
 

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;

解决方案

Think this should work:

YourModel.last.id + 1

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

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