Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All versions have the same created_at value #588

Closed
khelll opened this issue Aug 11, 2015 · 5 comments
Closed

All versions have the same created_at value #588

khelll opened this issue Aug 11, 2015 · 5 comments
Milestone

Comments

@khelll
Copy link

khelll commented Aug 11, 2015

Every version gets created with the same created_at value of the first version.

This is my model:

class Post < ActiveRecord::Base
  has_paper_trail only: [:title, :content, :link], on: [:update]
end
@jaredbeck
Copy link
Member

Every version gets created with the same created_at value of the first version.

  • Are you talking about the versions.created_at column, or the created_at value serialized in versions.object?
  • What version of paper_trail are you using?
  • What is the minimum code I can paste into a brand new rails app to reproduce this problem?

@ItaiLeibowitz
Copy link
Contributor

I believe I'm running into the same issue, using PaperTrail 4.0.0:

This is in record_create, where data[PaperTrail.timestamp_field] is set to the item's created_at value. The problem is, if the item was actually created at some point in the past and the current version is actually re-creating it (because it has also been deleted at some point), the version is getting a bad timestamp that is not in line with the current time of the update.

Here's an illustration of the problem I'm getting because of this. I'm implementing an undo/redo mechanism:

Time=0: Item1.create => (item1.created_at = 0; item1.updated_at = 0; version0.created_at = 0 )
Time=1: Item1.update =>(Item1.created_at = 0; item1.updated_at = 1; version1.created_at = 1 )
Time=2: item1.destroy =>(item1 = nil; version2.created_at = 2)
Time=3: item1.create =>(item1.created_at = 0; item1.updated_at = 3; version3.created_at = 0)

You can see that version3 has the wrong time stamp. It's a problem for me because reify_has_many still only uses timestamps to find subsequent versions, and my item1 is a has_many association. when I undo/redo, version3 never comes up in the reify_has_many query and that messes up the reification of associations.

One potential suggestion - it might be better to use the item's updated_at field for the timestamp - I think that would solve this problem:

def record_create 
[...]
  if respond_to?(:updated_at)
    data[PaperTrail.timestamp_field] = updated_at
  end
[...]

same as in the record_update method.

@khelll
Copy link
Author

khelll commented Aug 11, 2015

I found the problem, turns out the method updated_at was redefined in the Post model. It seems that the gem is making use of it to store the created_at for the version.

@batter
Copy link
Collaborator

batter commented Aug 11, 2015

I believe PR #589 will address this if / when merged, correct?

@ItaiLeibowitz
Copy link
Contributor

Yes, exactly right.

@batter batter closed this as completed in 7d7af89 Aug 12, 2015
@batter batter added this to the 4.1.0 milestone Aug 12, 2015
@jaredbeck jaredbeck modified the milestones: 4.0.1, 4.1.0 Dec 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants