diff --git a/CHANGELOG.md b/CHANGELOG.md index 79038dd07..644c0fef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ ### Fixed +- [#777](https://github.com/airblade/paper_trail/issues/777) - + Support HMT associations with `:source` option. - [#731](https://github.com/airblade/paper_trail/pull/731) - Map enums to database values before storing in `object_changes` column. - [#715](https://github.com/airblade/paper_trail/issues/715) - diff --git a/lib/paper_trail/reifier.rb b/lib/paper_trail/reifier.rb index 7d3edbc3c..473da1580 100644 --- a/lib/paper_trail/reifier.rb +++ b/lib/paper_trail/reifier.rb @@ -255,7 +255,7 @@ def reify_has_many_through(transaction_id, associations, model, options = {}) } else collection_keys = through_collection.map { |through_model| - through_model.send(assoc.association_foreign_key) + through_model.send(assoc.source_reflection.foreign_key) } version_id_subquery = assoc.klass.paper_trail_version_class. diff --git a/test/dummy/app/models/authorship.rb b/test/dummy/app/models/authorship.rb index ea16d84a2..fc5afa103 100644 --- a/test/dummy/app/models/authorship.rb +++ b/test/dummy/app/models/authorship.rb @@ -1,5 +1,5 @@ class Authorship < ActiveRecord::Base belongs_to :book - belongs_to :person + belongs_to :author, class_name: "Person" has_paper_trail end diff --git a/test/dummy/app/models/book.rb b/test/dummy/app/models/book.rb index f35d21baa..96ab910cc 100644 --- a/test/dummy/app/models/book.rb +++ b/test/dummy/app/models/book.rb @@ -1,6 +1,6 @@ class Book < ActiveRecord::Base has_many :authorships, dependent: :destroy - has_many :authors, through: :authorships, source: :person + has_many :authors, through: :authorships has_many :editorships, dependent: :destroy has_many :editors, through: :editorships diff --git a/test/dummy/app/models/person.rb b/test/dummy/app/models/person.rb index 45938261a..4a9ef1ea5 100644 --- a/test/dummy/app/models/person.rb +++ b/test/dummy/app/models/person.rb @@ -1,5 +1,5 @@ class Person < ActiveRecord::Base - has_many :authorships, dependent: :destroy + has_many :authorships, foreign_key: :author_id, dependent: :destroy has_many :books, through: :authorships has_paper_trail diff --git a/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb b/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb index 28f7faf9c..76f2bc0f9 100644 --- a/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +++ b/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb @@ -138,7 +138,7 @@ def up create_table :authorships, force: true do |t| t.integer :book_id - t.integer :person_id + t.integer :author_id end create_table :people, force: true do |t| diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index cc16254d1..f0a5a8d2c 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -27,7 +27,7 @@ create_table "authorships", force: :cascade do |t| t.integer "book_id" - t.integer "person_id" + t.integer "author_id" end create_table "banana_versions", force: :cascade do |t| diff --git a/test/unit/associations_test.rb b/test/unit/associations_test.rb index 91c6a9e75..6de9daf5b 100644 --- a/test/unit/associations_test.rb +++ b/test/unit/associations_test.rb @@ -523,7 +523,7 @@ class AssociationsTest < ActiveSupport::TestCase should "mark the newly associated-through for destruction" do assert @book_0. authorships. - detect { |as| as.person.name == "author_1" }. + detect { |as| as.author.name == "author_1" }. marked_for_destruction? end end @@ -564,7 +564,7 @@ class AssociationsTest < ActiveSupport::TestCase should "mark the newly associated-through for destruction" do assert @book_0. authorships. - detect { |as| as.person.name == "person_existing" }. + detect { |as| as.author.name == "person_existing" }. marked_for_destruction? end end diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index 728ee4aeb..acf8bc1b8 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -1025,7 +1025,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase @book.authorships.reload.last.destroy assert_equal 1, PaperTrail::Version.count - count assert_equal @book, PaperTrail::Version.last.reify.book - assert_equal @dostoyevsky, PaperTrail::Version.last.reify.person + assert_equal @dostoyevsky, PaperTrail::Version.last.reify.author end should "store version on join clear" do @@ -1034,7 +1034,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase @book.authorships.reload.destroy_all assert_equal 1, PaperTrail::Version.count - count assert_equal @book, PaperTrail::Version.last.reify.book - assert_equal @dostoyevsky, PaperTrail::Version.last.reify.person + assert_equal @dostoyevsky, PaperTrail::Version.last.reify.author end end