diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index feca1e0035..154c2d022c 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -590,8 +590,31 @@ class Fixtures fixtures.size end - def delete_existing_fixtures(table = table_name) - @connection.delete "DELETE FROM #{@connection.quote_table_name(table)}", 'Fixture Delete' + def delete_existing_fixtures + tables.each do |table| + @connection.delete "DELETE FROM #{@connection.quote_table_name(table)}", 'Fixture Delete' + end + end + + # Return a list of tables this fixture effect. This is typically the +table_name+ + # along with any habtm tables specified via Foxy Fixtures. + def tables + [table_name] + fixtures.values.map { |fixture| + row = fixture.to_hash + + # If STI is used, find the correct subclass for association reflection + associations = [] + if model_class && model_class < ActiveRecord::Base + reflection_class = row[inheritance_column_name].constantize rescue model_class + associations = reflection_class.reflect_on_all_associations + end + + foxy_habtms = associations.find_all { |assoc| + assoc.macro == :has_and_belongs_to_many && row.key?(assoc.name.to_s) + } + + foxy_habtms.map { |assoc| assoc.options[:join_table] } + }.flatten.uniq end def insert_fixtures @@ -667,10 +690,6 @@ class Fixtures @connection.insert_fixture(row, table_name) end - habtm_fixtures.keys.each do |table| - delete_existing_fixtures(table) - end - # insert any HABTM join tables we discovered habtm_fixtures.each do |table, fixtures| fixtures.each do |row| diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index ee51692f93..2a5d6b2beb 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -188,6 +188,11 @@ class FixturesTest < ActiveRecord::TestCase end end + def test_tables + fixtures = Fixtures.new(Parrot.connection, 'parrots', 'Parrot', FIXTURES_ROOT + "/parrots") + assert_equal %w{parrots parrots_treasures}, fixtures.tables + end + def test_yml_file_in_subdirectory assert_equal(categories(:sub_special_1).name, "A special category in a subdir file") assert_equal(categories(:sub_special_1).class, SpecialCategory)