In daily web app development, we often need to check if two model objects are equal. When I first started using Rails, this is what I wrote:

if ==
  # do something

Use obj1 == obj2 instead

Of course, that reflects my ignorance on learning the new tool.

So, here’s a proper way to compare if two model objects are equal:

test "2 records with same #id equal to each others." do
  c1 = Comment.create
  c2 = Comment.find(

  assert c1 == c2
  assert c2 == c1

What about new record?

Basically, a new model object only equals to itself:

test "New record does not equal to another new record." do
  new_record =
  another_new =

  assert new_record == new_record
  assert another_new == another_new
  assert new_record != another_new

Learning to use == operator for checking model equality is useful. Because now, we can start using other methods that depend on == operator. For instance, Array#include?.

test "When applied in Array" do
  c1 = Comment.create
  c2 = Comment.find(

  assert [c1].include? c2
  assert [c2].include? c1
  assert [c1, c2] == [c1, c2]

Rails learning tests

You may notice the code examples are written as tests. This is my first Rails learning test. So the plan is to write more tests as a way to learn Rails. I will post them here occationally if interesting enough to share.

Source code: model_equality_test.rb