Skip to main content

Convert SQL to ActiveRecord — and see the SQL Rails actually runs

ActiveRecord translations can silently change joins, filters, and results. Paste your SQL and compare it with the SQL Rails generates — before it breaks production.

Free online tool for Ruby developers • Supports JOINs, subqueries, and complex patterns • No registration required

New to ActiveRecord? Check out our comprehensive guide or browse practical examples

100% Free
No Sign-up Required
Instant Results

Note: Our converter handles most common SQL patterns accurately. For complex queries, please review and test the generated ActiveRecord code in your application.

Supports SELECT, INSERT, UPDATE, DELETE, JOINs, and subqueries

Tip: Always verify the generated SQL using .to_sql before relying on the result in production.

Popular Conversions

Common SQL patterns developers translate to ActiveRecord — with notes on where Rails behaves differently.

SQL Query:

Used 12 times

SELECT * FROM users WHERE id IN (1, 2, 3, 4, 5)

ActiveRecord:

User.where(id: [1, 2, 3, 4, 5])

SQL Query:

Used 11 times

SELECT * FROM wallets WHERE type = "CurrencyWallet" AND closed_at IS NULL AND NOT disabled_at IS NULL;

ActiveRecord:

Wallet.where(type: 'CurrencyWallet', closed_at: nil, disabled_at: nil)

SQL Query:

Used 11 times

SELECT * FROM users JOIN posts ON users.id = posts.user_id

ActiveRecord:

User.joins(:posts)

SQL Query:

Used 10 times

Select * from rails left joins upworks on upworks.rail_id = rails.id

ActiveRecord:

Rail.left_joins(:joins)

SQL Query:

Used 10 times

SELECT * FROM users 
  LEFT JOIN book b ON b.id = users.id_book
  RIGHT JOIN role r ON r.id = users.id_role
  INNER J...

ActiveRecord:

User.joins(:car).right_joins(:role).left_joins(:book)

Why SQL → ActiveRecord breaks in real Rails apps

LEFT JOINs → INNER JOINs

ActiveRecord's joins creates INNER JOINs by default, silently filtering out NULL associations and changing your result set.

includes() Side Effects

Using includes for N+1 prevention can accidentally change query semantics and return unexpected duplicate records.

DISTINCT Masking Issues

Adding distinct can hide broken joins that create Cartesian products, making "wrong" queries appear to work correctly.

GROUP/HAVING Gotchas

ActiveRecord's group and having methods can return "correct-looking" but semantically wrong counts and aggregations.

Supported Databases

Built for PostgreSQL-first Rails apps. MySQL and SQLite are supported for common query patterns:

PG

PostgreSQL

Advanced features like JSON queries, window functions, and complex joins.

PostgreSQL Guide →
MY

MySQL

Full support for MySQL syntax and specific database functions.

MySQL Guide →
SQ

SQLite

Perfect for development and testing environments.

See Guide →

Built for Rails

Generates Rails ActiveRecord code following proper conventions and best practices.

Instant Results

No setup, no registration, no waiting. Paste SQL and get production-ready ActiveRecord code in seconds.

Learn While Converting

Every conversion teaches Rails best practices. Perfect for teams learning ActiveRecord patterns.

SQL to ActiveRecord Conversion Examples

See how common SQL patterns translate to Rails ActiveRecord syntax:

Simple SELECT with WHERE

SELECT * FROM posts WHERE published = true AND created_at > '2024-01-01'

Converts to:

Post.where(published: true).where('created_at > ?', '2024-01-01')

INNER JOIN with Associations

SELECT users.name, posts.title FROM users INNER JOIN posts ON users.id = posts.user_id

Converts to:

User.joins(:posts).select('users.name, posts.title')

COUNT with GROUP BY

SELECT category, COUNT(*) FROM products GROUP BY category

Converts to:

Product.group(:category).count

ORDER BY with LIMIT

SELECT * FROM articles ORDER BY created_at DESC LIMIT 10

Converts to:

Article.order(created_at: :desc).limit(10)

Looking for specific SQL patterns? Explore our guides:

Why Use This Tool?

Fast & Simple

Convert SQL queries to ActiveRecord instantly without any setup or registration

Rails Best Practices

Generated code follows Rails conventions and ActiveRecord patterns

Learn ActiveRecord

Great for learning Rails patterns and migrating from other frameworks

Have Questions or Feedback?

We're constantly improving this tool based on user feedback. Found a bug? Have a feature request? Want to share your experience?