Change behavior of ` ==`

Hi everyone,

The active record documentation says the following:

Note that new records are different from any other record by definition, unless the other record is the receiver itself. Besides, if you fetch existing records with select and leave the ID out, you’re on your own, this predicate will return false.

But that’s really strange for me: ==

=> false

Since other data objects usually returns true if the containing data is equal: ==

=> true ==

=> true

Take a look on this code:

u1 = User.first = ‘John Doe’

u2 = User.first = ‘John Smith’

u1 == u2

=> true

The ActiveRecord::Base#== only compares the class and the id:

def ==(comparison_object)

super ||

comparison_object.instance_of?(self.class) &&
id.present? && == id


The method may be changed to compare the class and the attributes:

def ==(comparison_object)

super ||

comparison_object.instance_of?(self.class) &&
comparison_object.attributes == attributes


The latter will behave like other data objects on ruby.

I can overwrite this method only on my application or my model or even compare manually like this: ==

Regardless of that, the current behavior seems wrong.

There is a specific reason to the active record behavior be implemented in this way?

ActiveRecord defines identity using the DB primary key and class, if
there isn't a primary key it defaults to Ruby's object identity which
is if it is exactly the same object in memory.

String and OpenStruct have their own definitions of what object
equality means (both are based on content) so I don't think it relates
in any way to ActiveRecord, since it uses it's own definition of what
being == means.