lundi 28 décembre 2015

Inconsistent results from active-record query

This is my Person model which has the following query in a method:

def get_uniq_person_ids
    uniq_person_ids = select('person_id').where(:state => '1').uniq
    uniq_person_ids
end

My Test is as follows:

def test_uniqueness
    pids = Person.get_uniq_person_ids
    assert_equal(pids.size, 2)    
end

Test fails with the following:

Failure: <8> expected but was <2>.

There are 8 rows but only 2 unique person_id in the table.

This is what I tried:

  • puts pids before assert. It prints only 2 objects. Test fails with the above message.
  • binding.pry right before the query. Size is 2 which is expected and the test passes this time.

Why is the result so inconsistent? Is it a timing issue?

Note: I am using sqlite as my database. Also, I am doing this in a test so I wipe out all the data from the database before inserting the 8 rows in the table.

Aucun commentaire:

Enregistrer un commentaire