开发者

Rails primary key and object id

开发者 https://www.devze.com 2022-12-18 10:53 出处:网络
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.

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

It seem开发者_高级运维s 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?


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.

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


As mikej points out, you called id on a non-Active-Record. To verify, check out the object's class by using obj.class.

Please note, though, with duck-typing class doesn't matter... unless you think the object is a different class than it is :)

0

精彩评论

暂无评论...
验证码 换一张
取 消