rom-rb / rom

Data mapping and persistence toolkit for Ruby
https://rom-rb.org
MIT License
2.07k stars 161 forks source link

Associated entities are not mapped to their entity-classes in case of alias #673

Closed wuarmin closed 2 years ago

wuarmin commented 2 years ago

Describe the bug

I have a rom-sql project and use auto-struct with custom entity classes. That works perfectly. My repos return instances and their association-instances of my custom entity classes. However, as soon as I use the as option in an association definition, the association result is no longer mapped to the corresponding entity.

To Reproduce

module Orm
  module Relations
    class Posts < ROM::Relation[:sql]
      schema(:posts, infer: true) do
        associations do
          belongs_to :user, as: :create_user, foreign_key: :created_by
          belongs_to :user, as: :update_user, foreign_key: :updated_by
        end
      end
    end
  end
end
post = posts.combine(:create_user)
        .by_pk(1).one

expect(post).to be_a(Entities::Post) # success
expect(post.create_user).to be_a(Entities::User) # fails it is a instance of Entities::CreateUser

Expected behavior

expect(post.create_user).to be_a(Entities::User)

My environment

# ruby 2.7 Ubuntu
gem "rom", "~> 5.2"
gem "rom-sql", "~> 3.5"
wuarmin commented 2 years ago

That's a duplicate of #581. Sorry. But a solution would be great.

solnic commented 2 years ago

Thanks for reporting this, closing as a duplicate. It's scheduled for 6.0.

wuarmin commented 2 years ago

Looking forward to 6.0 👍