Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@janko
Created July 28, 2015 00:09
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 janko/2b2cea3e8e21d9232fb9 to your computer and use it in GitHub Desktop.
Save janko/2b2cea3e8e21d9232fb9 to your computer and use it in GitHub Desktop.
Insert statement in Arel
require "active_record"
ActiveRecord::Base.establish_connection("postgres:///db")
insert = Arel::Nodes::InsertStatement.new
insert.relation = Arel::Table.new(:movies)
insert.columns = hash.keys.map { |k| Arel::Table.new(:movies)[k] }
insert.values = Arel::Nodes::Values.new(hash.values, insert.columns)
ActiveRecord::Base.connection.execute(insert.to_sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment