[RUBY] Run raw sql in Hanami to get results not in columns

read and map


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

reference

https://github.com/hanami/model/issues/477

Recommended Posts

Run raw sql in Hanami to get results not in columns
.sql file does not run in docker-compose
Throw raw SQL to a read replica in Rails
How to run JUnit in Eclipse
How to run Ant in Gradle
Issuing SQL consecutively results in com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
How to get parameters in Spark
How to get date data in Ruby
Do not accept System.in in gradle run
How to get the date in java
[JPA] Compare table1 and table2 to get and update data that does not exist in table2.
Sample code to get the values of major SQL types in Java + MySQL 8.0