Skip to main content

Free · No sign-up · Rails 8 ready

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

Paste your SQL and compare it with the SQL Rails generates, before it breaks production.

  • PostgreSQL, MySQL, SQLite
  • JOINs and subqueries
  • Instant results
sql → activerecord
Supports SELECT, INSERT, UPDATE, DELETE, JOINs and subqueries.
Try:

Verify the output with .to_sql before using it in production.

New to ActiveRecord? Read the guide or browse practical examples. Review generated code before shipping it.

Examples

Common conversions

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

SQL:

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

ActiveRecord:

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

SQL:

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

ActiveRecord:

User.joins(:posts)

SQL:

SELECT * FROM users
LEFT JOIN posts ON posts.user_id = users.id
WHERE posts.id IS NULL

ActiveRecord:

User.where.missing(:posts)

SQL:

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

ActiveRecord:

Product.group(:category).count

SQL:

SELECT * FROM articles
ORDER BY created_at DESC LIMIT 10

ActiveRecord:

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

Pitfalls

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.

User.joins(:orders)
INNER JOIN: users with no orders vanish
User.left_joins(:orders)
LEFT OUTER JOIN: they stay

includes() Side Effects

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

User.includes(:orders)
preload: 2 queries
…where(orders: { paid: true })
switches to eager_load: 1 JOIN

DISTINCT Masking Issues

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

Post.joins(:tags, :comments)
rows multiply per tag × comment
…distinct
duplicates hidden, join still wrong

GROUP/HAVING Gotchas

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

Order.group(:status).count
counts every row per status
…having("COUNT(*) > 1")
filters groups, not rows

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 →

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

What happens to your SQL

Your SQL is sent to our server to convert it. Successful conversions are saved to improve the converter and are not shown publicly. Do not paste secrets or private data.

🔒

Encrypted in transit

Requests use HTTPS. Conversion runs on our server.

🛡️

Analytics on usage

We use Google Analytics and first-party analytics. Stats cover SQL type and length.

🗂️

Conversions are saved

Successful conversions are stored, not shown publicly. Keep secrets out.

⚡

Instant results

Most conversions return in about a second.

Latest from Our Blog

Deep dives on SQL, ActiveRecord patterns, and Rails performance

View All Posts →
Two tables: pluck(:id).first fetches five rows, while pick(:id) adds LIMIT and fetches one row.

Rails pick vs pluck: LIMIT 1 in ActiveRecord

pick is pluck for one row. See the SQL for pick vs pluck.first, why ORDER BY matters, and when to use find_by or exists? instead.

• 5 min read
Diagram of a four-row table reduced to two rows by DISTINCT, with a red ORDER BY arrow pointing at the removed created_at column.

SQL SELECT DISTINCT in Rails ActiveRecord: distinct, DISTINCT ON and the ORDER BY Error

Convert SQL SELECT DISTINCT to ActiveRecord: distinct, pluck, DISTINCT ON and the PostgreSQL ORDER BY error, with tested Rails 8.1 examples.

• 6 min read
Rails joins vs includes — The Difference That Kills Performance

Rails joins vs includes — The Difference That Kills Performance

joins filters using SQL JOIN. includes prevents N+1 by eager loading. Using the wrong one creates hidden performance problems. Rails 7/8 with SQL output.

• 3 min read

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?