# Mapping nested entities and other transformation examples

**URL:** https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223
**Category:** Usage Stories
**Created:** [April 8, 2018, 8:34pm UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223 "2018-04-08T20:34:31Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)
#### Post date: [April 8, 2018, 8:34pm UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/1 "2018-04-08T20:34:31Z")

</div>

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. [rom-rb.org](http://rom-rb.org) 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 [rom-rb.org](http://rom-rb.org), but the mappers section is sparse, unless I missed where to see more examples.

Thanks!

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)
#### Post date: [April 9, 2018, 12:08pm UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/2 "2018-04-09T12:08:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)
#### Post date: [April 13, 2018, 7:01am UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/3 "2018-04-13T07:01:49Z")

</div>

@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.

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)
#### Post date: [April 17, 2018, 12:04pm UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/4 "2018-04-17T12:04:23Z")

</div>

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](http://rom-rb.org/4.0/learn/core/structs/#how-struct-classes-are-determined) for more info.

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

```ruby
users.combine(:tasks).node(:tasks) { |t| t.as(:todos) }

```

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:

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

```

---

<div class="post-metadata">

### Author: ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)
#### Post date: [April 17, 2018, 6:42pm UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/5 "2018-04-17T18:42:04Z")

</div>

@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?

---

<div class="post-metadata">

### Author: ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)
#### Post date: [April 17, 2018, 11:22pm UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/6 "2018-04-17T23:22:23Z")

</div>

@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: [https://github.com/joelvh/rails\_event\_store/commit/5a9809cdee2f34f12294658faf6a10596ee977f7](https://github.com/joelvh/rails_event_store/commit/5a9809cdee2f34f12294658faf6a10596ee977f7)

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)

Thanks!

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

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)
#### Post date: [April 27, 2018, 8:42am UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/7 "2018-04-27T08:42:36Z")

</div>

> [@joelvh](#):
>
> 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[…].

This probably means that the order of mapping was not correct, ie you mapped to custom objects prior auto-mapping. Please make sure you tried it with latest `rom-core` **and** `rom-mapper` because this is exactly the issue I fixed recently.

> [@joelvh](#):
>
> How can I call the mapper by it’s registered name when I have an array instead of a relation?

Just call it, mappers work with anything array-like.

> [@joelvh](#):
>
> 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)

Yes, via `node(:foo) { |foo| foo.map_to(CustomClass) }` like I showed before or by using a custom struct with attribute set to `CustomClass` (assuming you’re using rom/struct or dry/struct).

> [@joelvh](#):
>
> 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.

If it bothers you, define a mapper that hides it 🙂

---

<div class="post-metadata">

### Author: ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)
#### Post date: [April 27, 2018, 1:33pm UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/8 "2018-04-27T13:33:22Z")

</div>

Thanks @solnic. Would it be appropriate to post some transformation examples to get feedback on how to best translate them to the ROM way or optimize them if they’re an attempt to use ROM? I have various examples that I can show in plain Ruby that would be helpful to see how you’d approach them, as I haven’t found many examples in the wild.

Thanks!

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)
#### Post date: [April 27, 2018, 5:31pm UTC](https://discourse.rom-rb.org/t/mapping-nested-entities-and-other-transformation-examples/223/9 "2018-04-27T17:31:37Z")

</div>

> [@joelvh](#):
>
> Would it be appropriate to post some transformation examples to get feedback on how to best translate them to the ROM way or optimize them if they’re an attempt to use ROM?

Yes no problem 🙂
