Write Inside Loop (ARCH6052)
Key ruby.orm.write-inside-loop · Severity warning · Category orm
Calling save/commit on every loop iteration produces one DB round-trip per item. That means N round-trips instead of a single batch operation.
Incorrect
Section titled “Incorrect”names.each do |name| User.create!(name: name) # a separate INSERT per iterationendCorrect
Section titled “Correct”records = names.map { |name| { name: name } }User.insert_all(records) # one bulk INSERTAccumulate the records and write them once with a bulk API like insert_all instead of persisting per row.