diff --git a/activerecord/lib/active_record/fixture_set/table_rows.rb b/activerecord/lib/active_record/fixture_set/table_rows.rb index e8335a2e10dedb6e9bb80a9f9036424570ea8991..3e3c0bc7ab15de34fc9132f9422923dbb73fffa8 100644 --- a/activerecord/lib/active_record/fixture_set/table_rows.rb +++ b/activerecord/lib/active_record/fixture_set/table_rows.rb @@ -45,6 +45,9 @@ def initialize(table_name, model_class:, fixtures:, config:) # track any join tables we need to insert later @tables = Hash.new { |h, table| h[table] = [] } + # ensure this table is loaded before any HABTM associations + @tables[table_name] = nil + build_table_rows_from(fixtures, config) end diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 82ca15b4153226c0361b51c4bef25c25978b6d4a..1092b9553fc2d4677680924954da4a9539e89058 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -619,6 +619,10 @@ def test_has_many_through_with_renamed_table assert_equal load_has_and_belongs_to_many["parrots_treasures"], rows["parrot_treasures"] end + def test_has_and_belongs_to_many_order + assert_equal ["parrots", "parrots_treasures"], load_has_and_belongs_to_many.keys + end + def load_has_and_belongs_to_many parrot = make_model "Parrot" parrot.has_and_belongs_to_many :treasures