I have Hanami models User
and UserInfo
that have has_one
association.
Repositories look the following:
class UserInfoRepository < Hanami::Repository
end
class UserRepository < Hanami::Repository
associations do
has_one :user_info
end
end
Question: who can I join and load both tables with one query? (I am looking for something similar to Rails' includes
).
So far I've tried
def users_with_info
users.join(:user_info)
end
It does the join, but does not select columns from user_infos
table.
Thanks in advance.
When you fetch data via a Repository in Hanami the result set is mapped into entities. By default the
UserRepository
will map to theUser
entity. I'm guessing that that entity does not have attributes for the columns fromuser_info
.What you need to do is create an Entity that can contain the data that you want to fetch from the database and then call
.as(ThatEntityYouCreated)
on the result set. E.g.,If you do not want to create an Entity and just want to get a plain hash, you can do this:
However, I consider this to be a crutch. You should not return Hashes from your Repository methods.