Skip to main content

Convert SQL INSERT to Rails ActiveRecord

Transform SQL INSERT statements into Rails ActiveRecord create operations

Supports single records, bulk inserts, and batch operations • 100% Free • No registration required

Try it now:

Common INSERT to ActiveRecord Conversions

Single Record Insert

SQL INSERT:

INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')

ActiveRecord:

User.create(name: 'John Doe', email: 'john@example.com')

Bulk Insert

SQL INSERT:

INSERT INTO users (name, email) VALUES ('John', 'john@example.com'), ('Jane', 'jane@example.com')

ActiveRecord:

User.insert_all([
  { name: 'John', email: 'john@example.com' },
  { name: 'Jane', email: 'jane@example.com' }
])

INSERT with Default Values

SQL INSERT:

INSERT INTO posts (title, user_id, created_at) VALUES ('New Post', 1, NOW())

ActiveRecord:

Post.create(title: 'New Post', user_id: 1)

Upsert Operations

SQL INSERT:

INSERT INTO users (email, name) VALUES ('john@example.com', 'John') ON CONFLICT DO NOTHING

ActiveRecord:

User.find_or_create_by(email: 'john@example.com') do |user|
  user.name = 'John'
end

Common INSERT Pitfalls When Converting to ActiveRecord

ActiveRecord callbacks fire on create — raw SQL bypasses them

When you run INSERT INTO users ... directly in SQL, ActiveRecord callbacks like before_create, after_create, and validations are completely skipped. User.create(...) runs all callbacks and validations; User.insert_all([...]) skips them for performance. Choose based on whether callbacks matter for your data.

Mass assignment protection requires permitted params

SQL INSERT can set any column directly. In Rails, User.create(params) only works if those attributes are permitted via strong parameters. Attempting to mass-assign a protected attribute raises ActiveModel::ForbiddenAttributesError. Always use params.require(:user).permit(:name, :email) before passing to create in controller actions.

insert_all vs create: performance and return values differ

For bulk inserts, User.create([...]) issues one SQL query per record and runs callbacks — extremely slow for large datasets. User.insert_all([...]) issues a single SQL INSERT with multiple rows, skips callbacks, and returns an ActiveRecord::Result — not model instances. Use upsert_all if you need ON CONFLICT behavior.

created_at and updated_at are set automatically

Unlike raw SQL where you must explicitly pass NOW() for timestamp columns, ActiveRecord sets created_at and updated_at automatically on every create. You do not need to include them in your attribute hash — and if you do pass them explicitly, ActiveRecord will use your value instead, which can cause subtle data inconsistencies.

Ready to Convert Your INSERT Statements?

Transform SQL inserts into efficient ActiveRecord operations