Skip to content

Commit

Permalink
Merge pull request #11461 from kennyj/fix_11454
Browse files Browse the repository at this point in the history
Fixes #11454 . We should define the return type of select_all method clearly.
  • Loading branch information
senny committed Jul 22, 2013
2 parents 68bbbd4 + dc1239d commit 2d50bc2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def to_sql(arel, binds = [])
end
end

# Returns an array of record hashes with the column names as keys and
# column values as values.
# Returns an ActiveRecord::Result instance.
def select_all(arel, name = nil, binds = [])
select(to_sql(arel, binds), name, binds)
end
Expand Down Expand Up @@ -355,8 +354,7 @@ def subquery_for(key, select)
subselect
end

# Returns an array of record hashes with the column names as keys and
# column values as values.
# Returns an ActiveRecord::Result instance.
def select(sql, name = nil, binds = [])
end
undef_method :select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ def exec_query(sql, name = 'SQL', binds = [])

alias exec_without_stmt exec_query

# Returns an array of record hashes with the column names as keys and
# column values as values.
# Returns an ActiveRecord::Result instance.
def select(sql, name = nil, binds = [])
exec_query(sql, name)
end
Expand Down
28 changes: 26 additions & 2 deletions activerecord/lib/active_record/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -62,6 +85,7 @@ def initialize_copy(other)
end

private

def hash_rows
@hash_rows ||=
begin
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ def test_disable_referential_integrity
end
end
end

def test_select_all_always_return_activerecord_result
result = @connection.select_all "SELECT * FROM posts"
assert result.is_a?(ActiveRecord::Result)
end
end

class AdapterTestWithoutTransaction < ActiveRecord::TestCase
Expand Down

0 comments on commit 2d50bc2

Please sign in to comment.