Skip to content

Instantly share code, notes, and snippets.

@shyam-habarakada
Forked from lankz/insert_sql.rb
Created April 14, 2016 03:34
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 shyam-habarakada/38987e134c6feb352c33bf5a7527e3c5 to your computer and use it in GitHub Desktop.
Save shyam-habarakada/38987e134c6feb352c33bf5a7527e3c5 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