Skip to content

Instantly share code, notes, and snippets.

@Mask
Created April 17, 2016 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mask/29e2e3d286183277d2f391aafc997025 to your computer and use it in GitHub Desktop.
Save Mask/29e2e3d286183277d2f391aafc997025 to your computer and use it in GitHub Desktop.
reify_has_many_through doesn't consider :source option
# Use this template to report PaperTrail bugs.
# It is based on the ActiveRecord template.
# https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
ruby "2.2.3"
source "https://rubygems.org"
gem "activerecord", "4.2.6"
gem "minitest", "5.8.3"
gem "paper_trail", git: 'https://github.com/airblade/paper_trail.git', require: false
gem "sqlite3"
gem "byebug"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.text :first_name, null: false
t.timestamps null: false
end
create_table :friendships, force: true do |t|
t.integer :user_id, null: false
t.integer :to_id, null: false
t.timestamps null: false
end
create_table :versions do |t|
t.string :item_type, null: false
t.integer :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.text :object, limit: 1_073_741_823
t.text :object_changes, limit: 1_073_741_823
t.integer :transaction_id
t.datetime :created_at
end
add_index :versions, [:item_type, :item_id]
add_index :versions, [:transaction_id]
create_table :version_associations do |t|
t.integer :version_id
t.string :foreign_key_name, null: false
t.integer :foreign_key_id
end
add_index :version_associations, [:version_id]
add_index :version_associations, [:foreign_key_name, :foreign_key_id],
name: "index_version_associations_on_foreign_key"
end
# Require `paper_trail.rb` after the `version_associations` table
# exists so that PT will track associations.
require "paper_trail"
# Example: we have a self-referencing has_many :through association:
# A User can have many other Users as friends.
# The problem here is the ':source' option.
class User < ActiveRecord::Base
has_paper_trail
has_many :friendships
has_many :friends, through: :friendships, source: :to
end
class Friendship < ActiveRecord::Base
has_paper_trail
belongs_to :user
belongs_to :to, class_name: 'User'
end
# Please write a test that demonstrates your issue by failing.
class BugTest < ActiveSupport::TestCase
def test_1
alice = nil
User.transaction do
alice = User.create(first_name: "Alice")
alice.friends.create(first_name: "Bob")
alice.touch_with_version
end
assert_equal(['Bob'], alice.friends.map { |f| f.first_name })
User.transaction do
alice.friends.create(first_name: 'Charlie')
alice.touch_with_version
end
assert_equal(['Bob', 'Charlie'], alice.friends.map { |f| f.first_name })
last_version = alice.versions.last
past_alice = last_version.reify(has_many: true)
assert_equal(['Bob'], past_alice.friends.map { |f| f.first_name }, 'Why is Alice a friend of herself suddenly?')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment