Hacker News new | ask | show | jobs
by JohnBooty 2583 days ago
From what I've seen of Sequel is looks awesome and I've been dying for an excuse to use it.

    found [ActiveRecord] to be severely limited in 
    working with complex queries which use many 
    JOIN statement and sub-queries
I'd humbly suggest that this is not a flaw of ActiveRecord whatsoever! I think this is AR working exactly as designed.

I would certainly agree that SQL > AR once your queries grow past a certain (fairly low) complexity threshold.

But, AR (wisely) doesn't try to be a complete replacement for SQL. It very happily (and even elegantly, I might say?) lets you use raw SQL pretty much any time you, the developer, feel it's more convenient/productive. That principle is pretty explicitly baked into AR.

Many ORM frameworks don't make this easy at all, whereas with AR it's very painless.

I have a lot of beef with AR but I think this is one of its strong points.

1 comments

Yes, I agree. For simple to low complexity queries AR is hard to beat, especially if your SQL knowledge is limited, and it allows for arbitrary SQL statements as string arguments.

My biggest issue with AR is that the arbitrary SQL cannot be combined or reused in a way which leads to compositional queries. An example would be to break up a large SQL statement into several smaller statements and then recombine them in different ways, safely.

What I found with Ecto and ROM is that, while simple to low complexity SQL was about the same as AR, maybe 10% harder to understand, complex to very complex statements were about equivalent to their SQL counterparts and could be composed together. That was a major win over AR for non-trivial queries.

> arbitrary SQL cannot be combined or reused in a way which leads to compositional queries

You can do this, look into arel. Here's a blog post:

https://blog.codeship.com/creating-advanced-active-record-db...

Yes, I know about Arel, and I’ve used it a few times to do hairy queries, but honestly it felt like swimming against the current and I wouldn’t recommend anyone write Arel queries by hand.
Yeah I think the easy composability of AR is probably its biggest asset.

As another commenter noted, yes you can do composable AR/Arel with raw SQL.... but like you said, it does get hairy pretty fast.