Skip to content

Instantly share code, notes, and snippets.

@boddhisattva
Created May 26, 2014 01:48
Show Gist options
  • Save boddhisattva/7719f628276da20dc334 to your computer and use it in GitHub Desktop.
Save boddhisattva/7719f628276da20dc334 to your computer and use it in GitHub Desktop.
Active Record Connection Exeute to return an array of hashes
2.0.0-p451 :036 > ActiveRecord::Base.connection.execute('SELECT * FROM microposts').each(as: :hash)
(0.3ms) SELECT * FROM microposts
=> [{"id"=>1, "content"=>"a", "user_id"=>1, "created_at"=>2014-05-24 09:50:57 UTC, "updated_at"=>2014-05-24 09:50:57 UTC},
{"id"=>2, "content"=>"", "user_id"=>1, "created_at"=>2014-05-24 09:51:02 UTC, "updated_at"=>2014-05-24 09:51:02 UTC},
{"id"=>3, "content"=>"", "user_id"=>1, "created_at"=>2014-05-24 09:51:06 UTC, "updated_at"=>2014-05-24 09:51:06 UTC},
{"id"=>4, "content"=>"", "user_id"=>2, "created_at"=>2014-05-24 09:51:09 UTC, "updated_at"=>2014-05-24 09:51:09 UTC},
{"id"=>5, "content"=>"", "user_id"=>3, "created_at"=>2014-05-24 09:51:13 UTC, "updated_at"=>2014-05-24 09:51:13 UTC},
{"id"=>6, "content"=>"", "user_id"=>4, "created_at"=>2014-05-24 09:51:17 UTC, "updated_at"=>2014-05-24 09:51:17 UTC},
{"id"=>7, "content"=>"", "user_id"=>4, "created_at"=>2014-05-24 09:51:22 UTC, "updated_at"=>2014-05-24 09:51:22 UTC},
{"id"=>8, "content"=>"", "user_id"=>1, "created_at"=>2014-05-24 09:51:26 UTC, "updated_at"=>2014-05-24 09:51:26 UTC},
{"id"=>9, "content"=>"", "user_id"=>2, "created_at"=>2014-05-24 09:51:30 UTC, "updated_at"=>2014-05-24 09:51:30 UTC}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment