4 ms·
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 a
by Dangeranger 7y ago
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.
- vinceguidry 7y ago> 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-queries-arel/ https://blog.codeship.com/creating-advanced-active-record-db...
- Dangeranger 7y agoYes, 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.
- JohnBooty 7y agoYeah 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.