What is an ORM?
An ORM (Object-Relational Mapper) is a tool that lets you interact with your database using your programming language instead of writing SQL.
Without an ORM, you’d write raw SQL to fetch data from your database: SELECT * FROM users WHERE active = true. With an ORM, you write code in your language instead: User.where(active: true). The ORM translates that into SQL and runs it for you. You get the same result, but you never have to leave your programming language.
The most well-known ORM in the Rails world is Active Record, which is built right into Rails. Each model in a Rails app corresponds to a database table, and Active Record gives you methods to query, create, update, and delete records without writing any SQL by hand. Other languages have their own ORMs: Hibernate for Java, SQLAlchemy for Python, and Prisma for JavaScript are popular examples.
I’ve used ORMs across a lot of languages over the years, and the benefit generally outweighs the trade-offs. Most of them have escape hatches that let you drop into raw SQL when you need to. The main thing to watch out for is performance, especially N+1 queries, where a loop inadvertently triggers a separate database query for each item. Most frameworks give you tools to profile and tune this when it comes up.