Skip to content

Instantly share code, notes, and snippets.

@ashanbrown
Last active August 29, 2015 14:01
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 ashanbrown/c5cf589d4fb8994afd8b to your computer and use it in GitHub Desktop.
Save ashanbrown/c5cf589d4fb8994afd8b to your computer and use it in GitHub Desktop.
test case for polymorphic inverse_of
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 :users do |t|
end
create_table :comments do |t|
t.integer :commentable_id
t.string :commentable_type
t.integer :user_id
end
end
class User < ActiveRecord::Base
has_many :comments, as: :commentable #, inverse_of: false # enable this to get test to pass
end
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
belongs_to :user
end
class BugTest < MiniTest::Unit::TestCase
def test_polymorphic_inverse_of_with_matching_association_name
user = User.create!
commenting_user = User.create!
comment = user.comments.create!
comment.user = commenting_user
comment.save
assert_equal commenting_user.id, comment.user.id
assert_equal commenting_user.id, user.comments.first.user_id
assert_equal commenting_user.id, user.comments.first.user.id # this assertion fails in rails4.1 but passes in rails4.0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment