开发者

Rails primary key and object id

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜