Last active
February 27, 2017 20:00
-
-
Save iwz/ed4643e20b0d23b2c8a8080f49a05f25 to your computer and use it in GitHub Desktop.
has_many :through must be defined after the has_many it needs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
source "https://rubygems.org" | |
# Activate the gem you are reporting the issue against. | |
gem "activerecord", "5.0.1" | |
gem "sqlite3" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# 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 "viewable_products", force: :cascade do |t| | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
end | |
create_table "viewable_products_group_components", force: :cascade do |t| | |
t.integer "viewable_product_id", null: false | |
t.integer "viewable_products_group_id", null: false | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
t.index ["viewable_product_id"], name: "index1", using: :btree | |
t.index ["viewable_products_group_id"], name: "index2", using: :btree | |
end | |
create_table "viewable_products_groups", force: :cascade do |t| | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
end | |
end | |
class ViewableProduct < ActiveRecord::Base | |
has_many :viewable_products_group_components | |
has_many :viewable_products_groups, through: :viewable_products_group_components | |
end | |
class ViewableProductsGroup < ActiveRecord::Base | |
has_many :viewable_products, through: :viewable_products_group_components | |
has_many :viewable_products_group_components # notice the :through is defined first, accidentally | |
end | |
class ViewableProductsGroupComponent < ActiveRecord::Base | |
belongs_to :viewable_product | |
belongs_to :viewable_products_group | |
end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
group = ViewableProductsGroup.new | |
group.viewable_products << ViewableProduct.new | |
group.save | |
assert_equal true, group.valid? | |
assert_equal 1, group.viewable_products.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To fix this failing test, simply move line 54 up one line, so that the
has_many :viewable_products_group_components
is defined beforehas_many :viewable_products, through: :viewable_products_group_components