diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 154c2d022c7abe636c3412259d40798be7e6db79..75c5daaaae77efc8a63d23e7da5512dc03b22653 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -617,7 +617,9 @@ def tables }.flatten.uniq end - def insert_fixtures + # Return a hash of rows to be inserted. The key is the table, the value is + # a list of rows to insert to that table. + def rows now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now now = now.to_s(:db) @@ -625,9 +627,9 @@ def insert_fixtures fixtures.delete('DEFAULTS') # track any join tables we need to insert later - habtm_fixtures = Hash.new { |h,k| h[k] = [] } + rows = Hash.new { |h,table| h[table] = [] } - rows = fixtures.map do |label, fixture| + rows[table_name] = fixtures.map do |label, fixture| row = fixture.to_hash if model_class && model_class < ActiveRecord::Base @@ -674,7 +676,7 @@ def insert_fixtures if (targets = row.delete(association.name.to_s)) targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/) table_name = association.options[:join_table] - habtm_fixtures[table_name].concat targets.map { |target| + rows[table_name].concat targets.map { |target| { association.foreign_key => row[primary_key_name], association.association_foreign_key => Fixtures.identify(target) } } @@ -685,15 +687,13 @@ def insert_fixtures row end + rows + end - rows.each do |row| - @connection.insert_fixture(row, table_name) - end - - # insert any HABTM join tables we discovered - habtm_fixtures.each do |table, fixtures| - fixtures.each do |row| - @connection.insert_fixture(row, table) + def insert_fixtures + rows.each do |table_name, rows| + rows.each do |row| + @connection.insert_fixture(row, table_name) end end end