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


#1

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


#2

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:

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

…which is obviously weird, but should work :wink:


#3

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.


#4

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

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:

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

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