Hanami: Simplified Models through Repositories and ROM

In a nutshell, simpler abstractions with less pitfalls.

I think any of us who’ve worked with Active Record and traditional Rails models have experienced at times dense and difficult syntax mixing abstractions for data validation, storage, querying and more.

Hanami with ROM eliminates those hours of Active Record fine-tuning.

ROM also removes the possibility of unintentional N+1 queries, where you make a database query per record by accident, with major performance bottlenecks.

By accessing combined relations through performant defaults (.

combine syntax), you never forget to preload or join and thus slow down your app.

On top of that, while there’s a learning curve even with similar code syntax/style, ROM has cleanly separated data access and manipulation logic from database-level operations.

Interestingly, ROM offers no callbacks of its own, in contrast to Active Record.

So if you’re used to before_validation, after_create or other Rails model code, you’ll have to utilize alternate approaches.

Avoiding callbacks in general is a best practice, producing less “magical” code with fewer steps to reason about.

There’s less magic and much more specific, intentional code in Hanami with ROM, and I find that to be a selling point.

Final ThoughtsSo there you have it: Hanami, our lighter weight yet still fully-featured Ruby framework, one you may not have considered for your side project or your startup’s dev team.

Along with ROM and Sequel, Hanami offers a simpler, more modular approach to the model layer than Rails developers have experienced over at times a decade plus of experience coding in that framework.

My experience with Hanami has been all-around positive, with enough similarity to Rails to get moving fast as a dev, and thoughtful abstraction choices throughout the framework that make a difference in developer productivity.

One aspect of coding I value most in my career is the way it makes me constantly question ways of thinking I take for granted.

This change in tech stack has made me reconsider in depth the many aspects of Ruby on Rails that I never questioned.

While there’s always a learning curve, I find myself greatly enjoying building new features with this new set of tools.

I highly recommend giving Hanami and ROM a try when you next have the opportunity.

Side Note: Hanami, Japanese EditionHanami (花見, “flower viewing”), in Japanese, comes around annually in the spring with picnic parties and food and drink under cherry blossom and plum blossom trees.

In Japan, for thousands of years people have enjoyed taking in the transient beauty of flowers, and to this day it’s a national event when cherry blossoms in particular bloom for a few weeks in the spring and arrive in each town and city.

It’s broadcast on TV as if it were a sporting event!Hanami was one of my favorite events living in Japan, and an aspect of Japanese culture that distilled the essence of a non-Western viewpoint on beauty, time, impermanence and more.

I hope Hanami the framework can live up to the millennia of history behind its namesake tradition!.

. More details

Leave a Reply