Skip to content

Instantly share code, notes, and snippets.

@dealforest
Created October 31, 2012 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dealforest/3987175 to your computer and use it in GitHub Desktop.
Save dealforest/3987175 to your computer and use it in GitHub Desktop.
ActiveRecord exexute sample
/* データ
> select * from hoge;
+------+------+
| fuga | foo |
+------+------+
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
+------+------+
*/
results = ActiveRecord::Base.connection.execute(<<"SQL")
SELECT * FROM hoge
SQL
results.each do |result|
p result.class
end
/*
[1, 2]
[3, 4]
[5, 6]
*/
results.each(:as => :hash) do |result|
p result.class
end
/*
{"fuga"=>1, "foo"=>2}
{"fuga"=>3, "foo"=>4}
{"fuga"=>5, "foo"=>6}
*/
p results.to_a
# [[1, 2], [3, 4], [5, 6]]
p results.to_a(:as => :hash)
# [{"fuga"=>1, "foo"=>2}, {"fuga"=>3, "foo"=>4}, {"fuga"=>5, "foo"=>6}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment