活动记录结果
此类封装了对任何数据库连接适配器调用 #exec_query 返回的结果。例如
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_a
# => [{"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
- #
- E
- I
- L
- N
- T
属性
[R] | column_types | |
[R] | columns | |
[R] | rows |
类公共方法
new(columns, rows, column_types = {}) 链接
源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 49 def initialize(columns, rows, column_types = {}) @columns = columns @rows = rows @hash_rows = nil @column_types = column_types end
实例公共方法
[](idx) 链接
源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 90 def [](idx) hash_rows[idx] end
each(&block) 链接
对行集合中的每个元素调用给定的块一次,将行作为参数传递。
如果没有给定块,则返回一个 Enumerator
。
源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 70 def each(&block) if block_given? hash_rows.each(&block) else hash_rows.to_enum { @rows.size } end end
empty?() 链接
如果没有记录,则返回 true,否则返回 false。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 79 def empty? rows.empty? end
includes_column?(name) 链接
如果此结果集包含名为 name
的列,则返回 true
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 57 def includes_column?(name) @columns.include? name end
initialize_copy(other) 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 133 def initialize_copy(other) @columns = columns.dup @rows = rows.dup @column_types = column_types.dup @hash_rows = nil end
last(n = nil) 链接
从行集合中返回最后一条记录。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 95 def last(n = nil) n ? hash_rows.last(n) : hash_rows.last end
length() 链接
返回行数组中的元素数量。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 62 def length @rows.length end
to_ary() 链接
返回一个数组,其中包含表示每条行记录的哈希。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/result.rb, line 84 def to_ary hash_rows end