# Aliased association doesn't map to the entity of the relation

**URL:** <https://discourse.rom-rb.org/t/aliased-association-doesnt-map-to-the-entity-of-the-relation/283>\
**Category:** Issues\
**Created:** [October 29, 2018, 2:13am UTC](https://discourse.rom-rb.org/t/aliased-association-doesnt-map-to-the-entity-of-the-relation/283 "2018-10-29T02:13:56Z")\
**Posts on this page:** 4\
**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:** [October 29, 2018, 2:13am UTC](https://discourse.rom-rb.org/t/aliased-association-doesnt-map-to-the-entity-of-the-relation/283/1 "2018-10-29T02:13:56Z")

</div>

Hi all,

I’ve got a relation called `identities`. I have another relation called `activities`. Activities has a `belongs_to` association called `by`, which is an alias for `identities`.

For some reason, the struct that `by` is mapped to is a `By` entity instead of `Identity` from the `identities` relation.

How do I get an assoociation to use the struct frrom the relation?

Thanks!

~Joel

---

<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:** [November 2, 2018, 4:28pm UTC](https://discourse.rom-rb.org/t/aliased-association-doesnt-map-to-the-entity-of-the-relation/283/2 "2018-11-02T16:28:13Z")

</div>

Struct inference uses relation names, so if they are aliased, which is what happens when you use `belongs_to`, a different name is used than the default one. You could workaround that like this:

```ruby
activities.combine(:by).node(:by) { |n| n.as(:identities) }

```

…which is obviously weird, but should work 😉

---

<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:** [January 22, 2019, 7:53am UTC](https://discourse.rom-rb.org/t/aliased-association-doesnt-map-to-the-entity-of-the-relation/283/3 "2019-01-22T07:53:42Z")

</div>

Thanks @solnic, sorry I didn’t reply sooner. Seems a little odd that there isn’t an option to specify what it should be. I think a saw someone else recently post about to this as well.

---

<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:** [February 12, 2019, 9:46am UTC](https://discourse.rom-rb.org/t/aliased-association-doesnt-map-to-the-entity-of-the-relation/283/4 "2019-02-12T09:46:41Z")

</div>

> [@joelvh](#):
>
> Thanks @solnic, sorry I didn’t reply sooner.

No worries, as you can see, I know how it goes 😃

> [@joelvh](#):
>
> Seems a little odd that there isn’t an option to specify what it should be

Adding a dedicated option that would specify which entity name should be used can be done relatively easily. I guess it could be called `struct_class`, so ie:

```ruby
belongs_to :identities, as: :by, struct_class: 'Identity'

```

Alternatively we could use an option that would prevent struct inferrer from using the alias.
