-
-
Save ochko/d8ecc6a73e58175a05f04421c977b979 to your computer and use it in GitHub Desktop.
Insert statement in Arel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insert_sql_for(table_name, data) | |
stmt = Arel::Nodes::InsertStatement.new | |
stmt.relation = Arel::Table.new(table_name) | |
stmt.columns = data.keys.map { |k| Arel::Table.new(table_name)[k] } | |
stmt.values = Arel::Nodes::Values.new(data.values, stmt.columns) | |
stmt.to_sql | |
end | |
require "active_record" | |
ActiveRecord::Base.establish_connection("postgres:///db") | |
ActiveRecord::Base.connection.execute(insert_sql_for :movies, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked for me on Rails 6