Rails 主键和对象 ID [英] Rails primary key and object id

查看:34
本文介绍了Rails 主键和对象 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用典型的主键 id 访问 Rails 模型.但是,当我在方法中访问它时,会收到以下警告.

I'm accessing a rails model with the typical primary key id. However, when I access it in a method, I get the following warning.

Object#id will be deprecated; use Object#object_id

似乎在对象 id 和模型的主键之间变得混淆了.有没有办法确保它使用字段 id?

It seems it's getting confused between object id, and the primary key for the model. is there a way to make sure its using the field id?

推荐答案

听起来您在其上调用 .id 的对象实际上并不是 ActiveRecord 模型.您应该只看到 Ruby 对象的警告,其中 .id 是即将被弃用的 Object#object_id 版本.

It sounds like the object you've called .id on is not actually an ActiveRecord model. You should only see that warning for Ruby objects where .id is the soon-to-be deprecated version of Object#object_id.

但是,使用 ActiveRecord 模型访问字段主键的另一种方法是 model.attributes['id'],所以您可以尝试一下.

However, another way to access the primary key for the field with an ActiveRecord model is model.attributes['id'] so you could try that.

这篇关于Rails 主键和对象 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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