在保存之前获取 Rails 模型的 ID...? [英] Get ID of Rails Model before saving...?

查看:30
本文介绍了在保存之前获取 Rails 模型的 ID...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在保存之前获取 Rails 模型的 id?

How do you get the id of a rails model before it is saved?

例如,如果我创建了一个新的模型实例,如何在保存之前获取其 ID?

For example, if I create a new model instance, how can I get its ID before it is saved?

我知道 ID 是在保存时根据数据库创建的,但有解决方法吗?

I know that the id is created onsave and according to the database but is there a workaround?

推荐答案

通常,当人们认为他们需要这样做时,他们实际上并不需要这样做.就像约翰说的那样,解释你想要做什么,有人可能会建议一种方法来做到这一点,而无需事先知道 ID.

Usually when people think they need to do this they actually do not need to do it. Like John says, explain what you are trying to do and someone can probably suggest a way to do it without having to know the id in advance.

这篇关于在保存之前获取 Rails 模型的 ID...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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