Ruby Object#id 警告和 Active Record [英] Ruby Object#id warnings and Active Record

查看:33
本文介绍了Ruby Object#id 警告和 Active Record的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在运行规范时不断看到如下警告:

We keep seeing warnings like the following when we run our specs:

Object#id 将被弃用;使用对象#object_id

Object#id will be deprecated; use Object#object_id

有问题的代码正在访问 ActiveRecord 模型的 id(显然,这是表上的一个属性,而不是 Ruby VM 中的对象实例 ID).

The code in question is accessing the id of an ActiveRecord model (which is an attribute on the table, obviously, rather than the object instance ID in the Ruby VM).

有谁知道如何关闭这些特定警告或以某种方式避免它们?

Does anyone know how to turn these particular warnings off or somehow avoid them?

推荐答案

我假设你在做模拟/存根(因为你提到了规范).

I'm assuming you're doing mocking / stubbing (because you mentioned specs).

就我而言,当我存根 ActiveRecord 对象并访问其 ID 属性时,我遇到了这些警告.

In my case, I run into these warnings when I stub an ActiveRecord object and access its ID attribute.

如果您希望访问 ActiveRecord 对象的 ID,我建议您执行以下操作:

In cases where you expect to access the ID of your ActiveRecord object, I'd recommend you do the following:

 mock("MyActiveRecordObject", :id => 1001)

这篇关于Ruby Object#id 警告和 Active Record的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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