Skip to content

Instantly share code, notes, and snippets.

@zinosama
Last active October 10, 2018 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zinosama/bdd5592789a5cbb2a307c102422f910b to your computer and use it in GitHub Desktop.
Save zinosama/bdd5592789a5cbb2a307c102422f910b to your computer and use it in GitHub Desktop.

When working with RSpec at Chowbus, we get failures like this (especially on our ubuntu CI server) from time to time:

# assuming persons table has created_at and updated_at
person = create(:person)
expect(Person.all).to contain_exactly(person)

This is because database (postgres in our case) and Ruby (ActiveRecord) object attribute keeps different precision of time:

# person is not loaded from database
person.created_at # 1539181279.1897986
# ActiveRecord loads this record from database
Person.first.created_at # 1539181279.1897979

Obviously, comparison by obj id is always more efficient and preferred. However, sometimes that would not be viable. When it's necessary to compare by object equality, we found a very simple solution:

person = create(:person).reload
# since person is now loaded from the database, it has the same time precision 
# as objects in Person.all.
expect(Person.all).to contain_exactly(person)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment