In Rails, an ActiveRecord model instance has an id
property that maps to the value stored in the id
column of the database. This may be nil
if the record hasn't been saved.
In Ruby, object_id
is a value that represents the identity of the object in question. It is always populated with something since everything in Ruby is an object.
These two are not related. There may be several independent instances of a model, each with their own object_id
value but an identical id
.
If two variables refer to something with the same object_id
, then they refer to exactly the same object.
It's rare to see object_id
used in code, it's a Ruby internal that's hardly ever needed. Mostly it's to establish if you're talking about identical objects, or just equivalent ones.
You will, on the other hand, see id
and similar values used frequently since that's the glue that holds your relational database together.