LoginSignup
2
0

More than 3 years have passed since last update.

Hanami(Rom)でsqlを実行してカラムにない結果を取得する

Last updated at Posted at 2020-10-30

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投げれる
わりとハマった

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0