Created
October 31, 2012 14:00
-
-
Save dealforest/3987175 to your computer and use it in GitHub Desktop.
ActiveRecord exexute sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* データ | |
> 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