class UserRepository < Hanami::Repository
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}
This will throw a complex query I was addicted to it
https://github.com/hanami/model/issues/477
Recommended Posts