Skip to content

Instantly share code, notes, and snippets.

@robinroestenburg
Created June 20, 2014 13:27
Show Gist options
  • Save robinroestenburg/693341f75c4f28904d7e to your computer and use it in GitHub Desktop.
Save robinroestenburg/693341f75c4f28904d7e to your computer and use it in GitHub Desktop.
Reproduction script: Wrong class using reflection on polymorphic belongs_to association
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
end
create_table :comments do |t|
t.integer :parent_id
t.string :parent_type
end
end
class Post < ActiveRecord::Base
has_many :comments, as: :parent
end
class Comment < ActiveRecord::Base
belongs_to :parent, :polymorphic => true
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!
post.comments << Comment.create!
assert_equal Post, Comment.reflect_on_all_associations(:belongs_to).first.klass
assert_equal Post, post.comments.first.association(:parent).reflection.klass
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment