Mapping nested entities


Hi all,

Been reading up on ROM and my first project is a ROM adapter for the RailsEventStore project. I am still getting my head around the shift from ActiveRecord to this paradigm, but ROM is what I’ve been looking for. has been super helpful!

As I learn about adjusting to different ways of querying data, I’m still looking for examples of simple shifts from AR to ROM. I’ll save those for another time.

Question: How do I map nested entities to a specific model, for certain queries in my repository class? As far as I can tell, I can use map_to or map_with for the root entity.

Where can I find more examples of mapping and moving from AR to ROM? I’ve used, but the mappers section is sparse, unless I missed where to see more examples.



Could you come up with some simple example (in terms of data structures and their source)?


@solnic the use case at the time was simply a many-to-one association and wanting to map the entity as well as the child object, each to their own class.

In retrospect, it sounds like maybe the mapper for the entity would also map the association attribute to the custom class for that association, potentially.


I’d recommend using auto-structs with custom namespaces and then name struct classes after relation names. Remember that you can alias relations at run-time too. See here for more info.

If you want to rename a relation that’s part of the graph you’re loading, you can do this:

users.combine(:tasks).node(:tasks) { |t| }

If you have a struct class in your configured namespace that’s called Todo, it’s gonna be used for mapping associated tasks.

If you want to use #map_to, you can do it in a similar way, ie:

users.combine(:tasks).node(:tasks) { |t| t.map_to(Todo) }


@solnic I’ve been using the auto-structs, but the node/as features are interesting as well.

I think the different strategies for mapping and where all you can do it is taking some time to understand. I would assume I can setup default mappers for relations or repositories, but haven’t had much luck doing that. Also, can Transproc DSL only be used in a custom mapper class, or elsewhere?


@solnic I’ve got an example of a commit where I’m replacing a SQL join with ROM combine. There’s the “StreamEntries” which reference an “Event” ID. I want to get a list of Event entities, but they need to be ordered based on “position” in the StreamEntry entity. I’ve been attempting to not intermingle relations, but it’s proven challenging, particularly when combined with mapping.

This commit shows the change from SQL joins to ROM combine:

I tried using your node mapping suggestion, but we’re mapping to a custom class and something was causing an error where the mapper or combine was trying to access attributes of the mapped class via event[...]. I was able to avoid mapping before combining by using the mapper outside the relation work.

  • How can I call the mapper by it’s registered name when I have an array instead of a relation?
  • Can a nested entity be mapped to a custom class? (I assume it has to be a Hash-like object because you store the combine key on the nested object)
  • I’d also love to not have to loop over the result by calling map to grab the nested “event” object, but imagine that’s probably the most practical way.

The only reason this relation work is inverted is because the ordering of the results matters, but the ordering is not on the entity that I want to return from the relation/repository.

Any suggestions on the approaches taken in the example? (This is the project I’m learning ROM with)


P.S. The tests pass, just want to see how you would do this differently.