Class: ActiveRecord::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_record/result.rb

Overview

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>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns, rows, column_types = {}) ⇒ Result

Returns a new instance of Result.



13
14
15
16
17
18
# File 'lib/active_record/result.rb', line 13

def initialize(columns, rows, column_types = {})
  @columns      = columns
  @rows         = rows
  @hash_rows    = nil
  @column_types = column_types
end

Instance Attribute Details

#column_typesObject (readonly)

Returns the value of attribute column_types.



11
12
13
# File 'lib/active_record/result.rb', line 11

def column_types
  @column_types
end

#columnsObject (readonly)

Returns the value of attribute columns.



11
12
13
# File 'lib/active_record/result.rb', line 11

def columns
  @columns
end

#rowsObject (readonly)

Returns the value of attribute rows.



11
12
13
# File 'lib/active_record/result.rb', line 11

def rows
  @rows
end

Instance Method Details

#[](idx) ⇒ Object



40
41
42
# File 'lib/active_record/result.rb', line 40

def [](idx)
  hash_rows[idx]
end

#eachObject



20
21
22
# File 'lib/active_record/result.rb', line 20

def each
  hash_rows.each { |row| yield row }
end

#empty?Boolean

Returns true if there are no records.

Returns:

  • (Boolean)


32
33
34
# File 'lib/active_record/result.rb', line 32

def empty?
  rows.empty?
end

#initialize_copy(other) ⇒ Object



48
49
50
51
52
# File 'lib/active_record/result.rb', line 48

def initialize_copy(other)
  @columns   = columns.dup
  @rows      = rows.dup
  @hash_rows = nil
end

#lastObject



44
45
46
# File 'lib/active_record/result.rb', line 44

def last
  hash_rows.last
end

#to_aryObject



36
37
38
# File 'lib/active_record/result.rb', line 36

def to_ary
  hash_rows
end

#to_hashObject



24
25
26
# File 'lib/active_record/result.rb', line 24

def to_hash
  hash_rows
end