Your application thinks in objects. Your database thinks in rows. An ORM — Object-Relational Mapping — is the bridge between them.
Without one, every query is three jobs: write the SQL, handle the result set, and turn each row into an object yourself. For every single query, kept in step with the schema by hand.
With Rails and Active Record, the same work looks like this:
user = User.find(1) → SELECT … WHERE id = 1
user.update(name: "Alex") → UPDATE users SET name …
User.create(name: "Sam") → INSERT INTO users …
One model maps to one table. Each call becomes SQL, and each row comes back as an object you can work with.
What an ORM does NOT do:
✘ It doesn't replace SQL
✘ Generated queries aren't automatically the efficient ones
Three things that separate people who use an ORM well from people who get burned by one:
→ Know the SQL it generates — read your logs, use `.to_sql`, turn on query logging in development
→ Watch for N+1 queries — `includes` exists for a reason
→ Stick to the safe query methods: `User.where(email: params[:email])` lets Active Record bind the value instead of interpolating it into a string
An ORM buys you less boilerplate and more readable code. It does not buy you permission to stop understanding your database.
—
#Astmation — #software #systems, #automation, and #AI-powered #applications.
Build. Automate. Evolve.
https://astmation.com/
Watch on YouTube