If you start hitting performance issues with Rails, switch to using Arel for queries and pump the data into lightweight classes. ActiveRecord optimization magic is a foot bazooka.
If you want to reuse the code between these objects and the corresponding ActiveRecord model, do it with a module.
The biggest problem with Rails is that people get so glued to the conventions that everything gets shoehorned into a model or controller instead of using a better abstraction.
The rails core team would tell you NOT to use arel, as it's private API.
The conceptual compression of having activerecord be a database table mapper, entity class, form/input validation, and query builder, it's both its greatest strength and its greatest weakness, depending of the stage of the business. FWIW the ruby community has more suitable alternatives which self serve the kind of abstractions you describe, such as hanami.
Back when I wrote Rails apps, Arel was stable enough and its own gem. Things may have changed, and that’s incredibly depressing.
Sometimes you just need SQL not everything that AR gives you. Most apps are read heavy, so you need an escape hatch. For me, Arel was always that: a programmatic way to alter SQL.
Hanami is a complete Rails replacement. I wouldn’t advocate for that at all.
As someone who evangeline Arel where I used to work 10y ago, I can assure you that arel has *never* been considered stable (it also never followed semver), nor has it ever been considered recommended or publicly documented API to use with rails, neither in the time prior to the gem having been moved to the github rails org, nor after. And every rails core team member that has publicly mentioned arel since, afair has always prefaced it with the "private API" warning.
> Sometimes you just need SQL not everything that AR gives you.
I used to agree, until I took a step back and realised how much ruby code ceremony I had to write in order to write, p.ex. A multi insert statement With an expression. It was several character more, and didn't seamlessly trabslate to SQL. I eventually replaced the AREL craft with core sequel, where I'd use it to build the queries and pass the statements to the AR sql execution method. I could have gone with raw sequel too.
> Hanami is a complete Rails replacement. I wouldn’t advocate for that at all.
The top level comment mentioned abstractions that something like hanami already supports. Instead of bending rails, to something it was not built to support, you either pick an alternative tool that does, or you accept what rails does and move on.
> The biggest problem with Rails is that people get so glued to the conventions that everything gets shoehorned into a model or controller instead of using a better abstraction.
https://speakerdeck.com/cgriego/models-models-every-where?sl...
objects are fundamentally a bad abstraction over sql. Thats why I love phoenix's ecto. its a very good elixir abstraction over sql that is more like a super powered Arel.
> The biggest problem with Rails is that people get so glued to the conventions that everything gets shoehorned into a model or controller instead of using a better abstraction.
As a Rails fanboy, I couldn't agree more.
Rails' semi-insistence on doing everything convention based and rewarding you for it can be a double edged sword in that way. The idea of doing anything outside the bounds of a controller/model/job sounds scary, especially when you consider how flexible Ruby itself is and how off the rails (heh) you can go pretty quickly. But at the end of the day, we should not be afraid of creating new conventions - which is a demonstrably different idea than just devolving into a raw Ruby scripting bonanza.