Execute sql script inside seed.rb in rails3

For multiple sql statements, I ended up iterating over each statement separately:

# db/seeds.rb

connection = ActiveRecord::Base.connection()

sql = <<-EOL
  INSERT INTO users values ('Admin');
  INSERT INTO categories values ('Supervisors');
EOL

sql.split(';').each do |s|
  connection.execute(s.strip) unless s.strip.empty?
end

Try this in db/seeds.rb to execute raw SQL with rake db:seed

connection = ActiveRecord::Base.connection()
connection.execute("*_YOUR_SQL_HERE_*")

A variation on Fredrik Boström's answer that can load from a file:

# db/seeds.rb

def execute_sql_file(path, connection = ActiveRecord::Base.connection)
  require 'active_support/core_ext/object/blank.rb'
  IO.read(path).split(';').reject(&:blank?).each do |statement|
    connection.execute(statement)
  end
end

execute_sql_file('my.sql')