aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorkennyj <kennyj@gmail.com>2013-07-22 21:30:55 +0900
committerkennyj <kennyj@gmail.com>2013-07-23 02:20:28 +0900
commitdc1239d96537c82fb7763e6bf4bb53f9dc00edf6 (patch)
tree3879e35c2357b1077eb3eee9a11c526cf70f4391 /activerecord
parent42f4ec62657efb002aa550ed4be6099ef13a6d50 (diff)
downloadrails-dc1239d96537c82fb7763e6bf4bb53f9dc00edf6.tar.gz
rails-dc1239d96537c82fb7763e6bf4bb53f9dc00edf6.tar.bz2
rails-dc1239d96537c82fb7763e6bf4bb53f9dc00edf6.zip
Added some usage about ActiveRecord::Result [ci skip]
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/result.rb28
1 files changed, 26 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb
index a7a035fe46..253368ae5b 100644
--- a/activerecord/lib/active_record/result.rb
+++ b/activerecord/lib/active_record/result.rb
@@ -3,8 +3,31 @@ module ActiveRecord
# This class encapsulates a Result returned from calling +exec_query+ on any
# database connection adapter. For example:
#
- # x = ActiveRecord::Base.connection.exec_query('SELECT * FROM foo')
- # x # => #<ActiveRecord::Result:0xdeadbeef>
+ # result = ActiveRecord::Base.connection.exec_query('SELECT id, title, body FROM posts')
+ # result # => #<ActiveRecord::Result:0xdeadbeef>
+ #
+ # # Get the column names of the result:
+ # result.columns
+ # # => ["id", "title", "body"]
+ #
+ # # Get the record values of the result:
+ # result.rows
+ # # => [[1, "title_1", "body_1"],
+ # [2, "title_2", "body_2"],
+ # ...
+ # ]
+ #
+ # # Get an array of hashes representing the result (column => value):
+ # result.to_hash
+ # # => [{"id" => 1, "title" => "title_1", "body" => "body_1"},
+ # {"id" => 2, "title" => "title_2", "body" => "body_2"},
+ # ...
+ # ]
+ #
+ # # ActiveRecord::Result also includes Enumerable.
+ # result.each do |row|
+ # puts row['title'] + " " + row['body']
+ # end
class Result
include Enumerable
@@ -62,6 +85,7 @@ module ActiveRecord
end
private
+
def hash_rows
@hash_rows ||=
begin