I have setup the Papertrail gem to track changes to my notes table. I want to show all the "updates" made to a note. I am doing this by:
Annotation.last.versions.each { |v| v.changeset }
This works, but is making unnecessary queries back to the Note table.
If a Note has N versions, then v.changeset does N queries to the Note table to get the same note. I assume this is because I have setup my relationship incorrectly. My set up is below.
class Note < ActiveRecord::Base
has_paper_trail(versions: { class_name: 'NoteVersion' })
End
class NoteVersion < PaperTrail::Version
belongs_to(:note, foreign_key: :item_id, inverse_of: :versions)
End
My versions table has the tables: item_type, item_id, object, object_changes and a few others.
Answering my own question, it always seems to happen you discover the answer just after posting!
It was my rails knowledge and how to setup polymorphic associations that was letting me down.
I could have changed the Version class to the below to correctly load the association.
Or what I ended up doing was including the papertrail version concern which already has this logic built in.
Hopefully that will help someone else