Skip to content

Commit

Permalink
Merge pull request #938 from elju/fix-belongs-to-foreign-key
Browse files Browse the repository at this point in the history
Fix belongs_to reify for associations that use a different foreign_key
  • Loading branch information
jaredbeck authored Mar 27, 2017
2 parents 7332d5c + 48f1194 commit 8a703d6
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
matchers to work with custom version association names
- [#929](https://github.com/airblade/paper_trail/pull/929) -
Fix error calling private method in rails 4.0
- [#938](https://github.com/airblade/paper_trail/pull/938) - Fix bug where
non-standard foreign key names broke belongs_to associations

## 6.0.2 (2016-12-13)

Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/reifiers/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module BelongsTo
class << self
# @api private
def reify(assoc, model, options, transaction_id)
id = model.send(assoc.association_foreign_key)
id = model.send(assoc.foreign_key)
version = load_version(assoc, id, transaction_id, options[:version_at])
record = load_record(assoc, id, options, version)
model.send("#{assoc.name}=".to_sym, record)
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/models/person.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Person < ActiveRecord::Base
has_many :authorships, foreign_key: :author_id, dependent: :destroy
has_many :books, through: :authorships
belongs_to :mentor, class_name: "Person", foreign_key: :mentor_id
has_paper_trail

# Convert strings to TimeZone objects when assigned
Expand Down
1 change: 1 addition & 0 deletions test/dummy/db/migrate/20110208155312_set_up_test_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def up
create_table :people, force: true do |t|
t.string :name
t.string :time_zone
t.integer :mentor_id
end

create_table :editorships, force: true do |t|
Expand Down
5 changes: 3 additions & 2 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@
end

create_table "people", force: :cascade do |t|
t.string "name"
t.string "time_zone"
t.string "name"
t.string "time_zone"
t.integer "mentor_id"
end

create_table "post_versions", force: :cascade do |t|
Expand Down

0 comments on commit 8a703d6

Please sign in to comment.