Skip to content

Instantly share code, notes, and snippets.

@CodyFitzpatrick
Last active November 14, 2022 11:17
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CodyFitzpatrick/d74d24f36f67b8a42fe6ee7cc6a58c55 to your computer and use it in GitHub Desktop.
Save CodyFitzpatrick/d74d24f36f67b8a42fe6ee7cc6a58c55 to your computer and use it in GitHub Desktop.
Ruby on Rails - Import Data via SQL in seeds.rb
# Creation of other records in Ruby above ...
connection = ActiveRecord::Base.connection
sql = File.read('db/import.sql') # Change path and filename as necessary
statements = sql.split(/;$/)
statements.pop
ActiveRecord::Base.transaction do
statements.each do |statement|
connection.execute(statement)
end
end
# Creation of other records in Ruby below ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment