Skip to content

Instantly share code, notes, and snippets.

@y-yagi
Forked from lankz/insert_sql.rb
Created July 29, 2016 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-yagi/4393b392b02654617ce6b5c48d50ff8e to your computer and use it in GitHub Desktop.
Save y-yagi/4393b392b02654617ce6b5c48d50ff8e to your computer and use it in GitHub Desktop.
record = Post.new(:title => 'Yay', :body => 'This is some insert SQL')
# easiest way to achieve this is by calling protected #arel_attributes_values (tested in
# rails 3.2.13). the alternative is to build the entire insert statement using arel >_>
record.class.arel_table.create_insert \
.tap { |im| im.insert(record.send(:arel_attributes_values, false)) } \
.to_sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment