Hacker News new | ask | show | jobs
by mobilemidget 590 days ago
I have seen many projects where people use activerecord for their SQL interfacing. It is only the really simple queries that are okay, but as soon as there is some complexity the generated sql query are not so good quality to put it politely. Imho raw sql is always best, build your own query generator which you understand its logic fully and is tailored for your project.
5 comments

I think I disagree with this.

Not about using raw SQL when needed. I would definitely do that for a hot, complex query if I felt I needed to but I think ActiveRecord is rock solid if you know how to use it and how to examine its output.

There are times where an ORM is a very useful tool and I've found ActiveRecord to be better than most home made query generators I’ve run into regardless of base language.

Absolutely not the case, in my experience.

Like any advanced tool, it’s possible to shoot yourself in the foot if you don’t use it correctly. But once you know it, it makes simple queries easy, complex queries tolerable, and allows progressively dropping to raw SQL if needed.

I’d go even further and say ActiveRecord (particularly when using Arel for complex query construction) is the single best ORM I’ve ever used. I miss it hard whenever I interact with an SQL database in any other language.

> Imho raw sql is always best, build your own query generator which you understand its logic fully and is tailored for your project.

This comes with the caveat that someone will have to work with bespoke-sql-codegen:0.0.1 when you go work in another company, with StackOverflow being of no help, as well as an unknown amount of vulnerabilities.

I once worked on a Java project that put like 90% of the logic in the DB and the application mostly called prepared statements and parsed the results with a bunch of low level logic. There was some dynamic SQL generation but the end result was the whole thing feeling like code archaeology because of course there was basically no documentation or good examples on how to do things, decoupled from the already complex codebase.

It was blazing fast but also an absolute nightmare to work with. I would be similarly guarded towards using anything bespoke without a community and knowledge base, regardless of the stack.

As for ORMs, there’s nothing preventing you from making an efficient DB view and mapping that into an entity.

Hard disagree. I have replaced hand written reports that were total nightmares to reason about and maintain with fairly trivial activerecord implementations that were quite literally hundreds of times faster.

Activerecord may not give optimal solutions but it can get close enough for a lot of workloads, and complicated sql can become a complete bear over time.

> It is only the really simple queries that are okay, but as soon as there is some complexity the generated sql query are not so good quality to put it politely.

ActiveRecord was specifically designed to work in that way—tools for most queries, which are simple, and making it easy to use SQL for more complicated cases.