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
Note: Our converter handles most common SQL patterns accurately. For complex queries, please review and test the generated ActiveRecord code in your application.
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:
PostgreSQL
Advanced features like JSON queries, window functions, and complex joins.
PostgreSQL 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:
Learn More About ActiveRecord
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?