ActiveRecordを使って生sqlを書く場合

Base.connection.select_all あたりは細かく知りたい

hash = ActiveRecord::Base.connection.select_all("select sum(sub1.diff) from (select diff from differences order by created_at desc limit 5) sub1").to_hash
puts hash[0]["diff"]