How to test for (ActiveRecord) object equality

前端 未结 6 1288
礼貌的吻别
礼貌的吻别 2020-11-29 05:55

In Ruby 1.9.2 on Rails 3.0.3, I\'m attempting to test for object equality between two Friend (class inherits from ActiveRecord::

6条回答
  •  死守一世寂寞
    2020-11-29 06:06

    Take a look at the API docs on the == (alias eql?) operation for ActiveRecord::Base

    Returns true if comparison_object is the same exact object, or comparison_object is of the same type and self has an ID and it is equal to comparison_object.id.

    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.

    Note also that destroying a record preserves its ID in the model instance, so deleted models are still comparable.

提交回复
热议问题