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

reify(has_many: true) not working for associations to models using STI #497

Closed
mwalsher opened this issue Mar 11, 2015 · 4 comments
Closed

Comments

@mwalsher
Copy link

Best to explain via example:

class Contact
  has_paper_trail
  has_many :contact_emails
  has_many :contact_phones
end

class ContactEmail < ContactCommunicationItem
  has_paper_trail
end

class ContactPhone < ContactCommunicationItem
  has_paper_trail
end

class ContactCommunicationItem < ActiveRecord::Base
  has_paper_trail
  belongs_to :contact, touch: true
end

>> c = Contact.create(name: "Test")
>> c.contact_emails.create(value: "[email protected]", label: "Work")
>> c = c.reload.versions.last.reify(has_many: true, mark_for_destruction: true)
# c is nil, reify does not work in this case
@batter
Copy link
Collaborator

batter commented Mar 13, 2015

This is the expected behavior.

This is due to the fact that versions for create events always return nil when reify is called upon them since PaperTrail stores the state of what the model looked like before the persistence of that state that caused the version to be generated.

Please read the Basic Usage section of the README.

@batter batter closed this as completed Mar 13, 2015
@mwalsher
Copy link
Author

Thanks for your response.

Shouldn't two versions of the contact exist at that point however, given that a contact_email was added to the contact? Especially given that I have touch: true enabled on the association. Otherwise the has_many reification is effectively useless. Think of the scenario from a user perspective:

  1. User creates contact.
  2. User updates contact with email address.
  3. User restores contact to the fist revision.
  4. Expected outcome is that the email address is no longer present.

That said however, even if I modify the example to include an update on the contact before adding the email address, the bug persists.

Revised example code:

class Contact
  has_paper_trail
  has_many :contact_emails
  has_many :contact_phones
end

class ContactEmail < ContactCommunicationItem
  has_paper_trail
end

class ContactPhone < ContactCommunicationItem
  has_paper_trail
end

class ContactCommunicationItem < ActiveRecord::Base
  has_paper_trail
  belongs_to :contact, touch: true
end

>> c = Contact.create(name: "Test")
>> c.update_attributes(name: "Test 2") 
# update event > revision created
>> c.contact_emails.create(value: "[email protected]", label: "Work")
>> c = c.reload.versions.last.reify(has_many: true, mark_for_destruction: true)
>> c.save
>> c.contact_emails.present?
=> true
# contact_email is not removed as would be expected

@batter
Copy link
Collaborator

batter commented Mar 13, 2015

No, as the Rails documentation states for that touch option

:touch
If true, the associated object will be touched (the updated_at/on attributes set to current time) when this record is either saved or destroyed. If you specify a symbol, that attribute will be updated with the current time in addition to the updated_at/on attribute.

The example you just put above would work, except that you don't have the autosave option enabled on the association.

If you want to properly use the mark_for_destruction feature, then you need to use the autosave option on the association, so I believe that is why it probably isn't working as you hope / expect.

@batter
Copy link
Collaborator

batter commented Mar 13, 2015

There is an open issue #463 for trying to implement a feature for an option to generate versions upon only changes to associations which I'm trying to figure out before the final 4.0.0 release, which sounds like what you may be after.

batter added a commit that referenced this issue Mar 14, 2015
batter added a commit that referenced this issue Mar 16, 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

2 participants