Rails primary key and object identifier

I am referring to a rail model with a typical id primary key. However, when I access it in a method, I get the following warning.

 Object#id will be deprecated; use Object#object_id 

It seems to get confused between the identifier of the object and the primary key for the model. is there any way to make sure it uses the id field?

+4
source share
2 answers

It looks like the object you named .id on is not really an ActiveRecord model. You should see this warning only for Ruby objects, where .id is soon an obsolete version of Object#object_id .

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

+5
source

As mikej points out, you called id on an inactive record. To check, check the class of the object using obj.class .

Note that with the duck set class it doesn't matter ... if you don't think the object is different from the class :)

+2
source

Source: https://habr.com/ru/post/1299906/


All Articles