airblade/paper_trail — Issue #131: version_at may incorrectly return nothing
Author: henrik
Created: 2012-02-15 10:20:29+00:00
State: open

Had an issue where `version_at` returned nothing but should have.

Turns out `version_at` uses `versions.created_at`, which may be slightly later than the update that caused that version (`item.updated_at`).

Don't know if this is particular to Postgres. Suppose it happens more if you have higher-resolution timestamps.

Example:

> item.versions.last.created_at_before_type_cast
=> "2012-02-13 14:53:39.583205"
> item.updated_at_before_type_cast
=> "2012-02-13 14:53:39.580887"

In our case, we stowed away the `updated_at` of one record in another (e.g. `item.campaign_updated_at`). When we later tried to use that to find the right version of the campaign, we got nothing, because it queried for versions with `created_at > item.campaign_updated_at`. It is supposed to find the next version (representing the state the campaign was in at the given time), but instead found the version created at almost the same time as this update, representing the previous state. Sometimes there is no previous state, and we get nil.

Took me a while to wrap my head around exactly why we got the nil, but it should be enough to notice that the timestamps don't match up and this can cause off-by-one-version issues with `version_at`.