diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index 5b9b9fb4..73dfd639 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -71,7 +71,7 @@ module PaperTrail def version_at(timestamp) # Because a version stores how its object looked *before* the change, # we need to look for the first version created *after* the timestamp. - version = versions.first :conditions => ['created_at > ?', timestamp], :order => 'created_at ASC, id ASC' + version = versions.after(timestamp).first version ? version.reify : self end diff --git a/lib/paper_trail/version.rb b/lib/paper_trail/version.rb index af5fa684..dad9b389 100644 --- a/lib/paper_trail/version.rb +++ b/lib/paper_trail/version.rb @@ -16,6 +16,12 @@ class Version < ActiveRecord::Base :order => "id DESC", } } + named_scope :after, lambda { |timestamp| { + :conditions => ['created_at > ?', timestamp], + # TODO: is this :order necessary, considering its presence on the has_many :versions association? + :order => 'created_at ASC, id ASC' + } } + # Restore the item from this version. # # This will automatically restore all :has_one associations as they were "at the time",