Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@formigarafa
Copy link

formigarafa commented Feb 22, 2016

thanks, and: Or for Rails 4

record = Post.new(:title => 'Yay', :body => 'This is some insert SQL')

record.class.arel_table.create_insert. \
  tap { |im| 
    im.insert(record.send(
      :arel_attributes_with_values_for_create,
      record.attributes.compact.keys
    ))
  }.to_sql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment