Optimize ActiveRecord::Result#last
If you only want the last element of a result set, there's no need to create all of hash_rows. Also, add a test.
This commit is contained in:
parent
af834fb2c0
commit
763de2ac1c
@ -81,7 +81,8 @@ def first
|
||||
end
|
||||
|
||||
def last
|
||||
hash_rows.last
|
||||
return nil if @rows.empty?
|
||||
Hash[@columns.zip(@rows.last)]
|
||||
end
|
||||
|
||||
def cast_values(type_overrides = {}) # :nodoc:
|
||||
|
@ -27,6 +27,11 @@ def result
|
||||
{'col_1' => 'row 1 col 1', 'col_2' => 'row 1 col 2'}, result.first)
|
||||
end
|
||||
|
||||
test "last returns last row as a hash" do
|
||||
assert_equal(
|
||||
{'col_1' => 'row 3 col 1', 'col_2' => 'row 3 col 2'}, result.last)
|
||||
end
|
||||
|
||||
test "each with block returns row hashes" do
|
||||
result.each do |row|
|
||||
assert_equal ['col_1', 'col_2'], row.keys
|
||||
|
Loading…
Reference in New Issue
Block a user