readしてmap
romの仕様っぽい
class UserRepository < Hanami::Repository
# class UserRepository < ROM::Repository[:users]
def join_posts
query = "SELECT users.id, posts.id as post_id FROM users JOINS posts"
users.read(query).map.to_a
end
def age_average
query = "SELECT avg(age) as age_average FROM users"
users.read(query).map.first
end
end
repo = UserRepository.new
repo.join_posts
=> [{:id=>1, :post_id=>2},
{:id=>2, :post_id=>3}]
repo.age_average
=> {:age_average=>33}
これで複雑なquery投げれる
わりとハマった
参考