Skip to content

Instantly share code, notes, and snippets.

@meuble
Last active March 12, 2020 15:34
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 meuble/c047837f9551c8d6b8e67523ef51f6e9 to your computer and use it in GitHub Desktop.
Save meuble/c047837f9551c8d6b8e67523ef51f6e9 to your computer and use it in GitHub Desktop.
# This file describe a bug found in Active Record while migrating an old
# application from rails 3.2 to rails 6.0.1.
#
# with active record > 6.0.0rc2, the line below raises the following error:
# ActiveModel::UnknownAttributeError: unknown attribute 'shop' for
# Shop::HABTM_Contents.
#
# The bug was introduced by the following commit:
# https://github.com/rails/rails/commit/3b04715cda
# (https://github.com/rails/rails/pull/36776)
# The commit aimed at fixing https://github.com/rails/rails/issues/36742
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# The test pass with activerecord <= 6.0.0rc2 and fails >= 6.0.0
gem 'activerecord', '6.0.0'
gem 'sqlite3', '~> 1.4'
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 :contents, force: true do |t|
t.string :type
end
create_table :contents_shops, force: true do |t|
t.integer :shop_id
t.integer :content_id
end
end
class Content < ActiveRecord::Base
has_and_belongs_to_many :shops,
foreign_key: :content_id,
association_foreign_key: :shop_id,
join_table: :contents_shops
end
class Shop < Content
# Uncomment this re-declaration to make the test pass on ActiveRecord > 6.0.0.rc2
# has_and_belongs_to_many :shops,
# foreign_key: :content_id,
# association_foreign_key: :shop_id,
# join_table: :contents_shops
has_and_belongs_to_many :contents,
foreign_key: :shop_id,
association_foreign_key: :content_id,
join_table: :contents_shops
end
class BugTest < Minitest::Test
def test_association
Shop.create!.shops = [Shop.create, Shop.create]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment