Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoStruct does not respect type definitions for associations #581

Open
cflipse opened this issue Jan 3, 2020 · 1 comment
Open

AutoStruct does not respect type definitions for associations #581

cflipse opened this issue Jan 3, 2020 · 1 comment

Comments

@cflipse
Copy link
Collaborator

cflipse commented Jan 3, 2020

When using auto_struct and a module definition, ROM is not respecting the configured types for an association.

schema(:articles, infer: true) do
  associations do
    belongs_to :profiles, as: :author, foreign_key: :author_id
  end
end

class Entities::Article < Entity      # entity itself inherits from ROM::Struct
  attribute :author,   ::Entities::Profile
end

ArticleRepository.new(rom).first.author.class
# => Entities::Author ... expect an Entities::Profile

reproduction script: https://gist.github.com/cflipse/947447e4c656b35d351ffb518df38d65

Directly mapping to the class via map_to will coerce the author field correctly.

@wuarmin
Copy link

wuarmin commented May 15, 2023

@cflipse Adding struct_namespace Entities to the ArticleRepository solved the issue for me:

class Articles < ROM::Repository[:articles]
  struct_namespace Entities

  def list
    articles.combine(:author)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants