Hi everyone,
I’m new to Hanami and trying to structure my application correctly. I need some guidance on how to properly share relations across slices.
Since relations define our low-level data sources, my understanding is that they should be defined once and reused in repositories across different slices. Is this the recommended approach? If so, how can I achieve this?
For example, I have a relation defined in app/relations/books.rb
, and I want to use it in my slices/web/repos/book_repo.rb
. However, when I try to reference it in the repository, I get the following error:
undefined local variable or method 'books' for an instance of Web::Repos::BookRepo
Here’s a simplified version of my setup:
Relation (Defined at the application level)
# /app/relations/books.rb
module MyApp
module Relations
class Books < MyApp::DB::Relation
schema :books, infer: true
end
end
end
Repository (Inside a slice)
# /slices/web/repos/book_repo.rb
module Web
module Repos
class BookRepo < Web::DB::Repo
def all
books.to_a
end
end
end
end
What is the correct way to access the books
relation inside my slice’s repository? Should I be explicitly importing or registering it somewhere?
Any help or best practices would be greatly appreciated!