jeremyevans/sequel — pull request #425 by crawlik (2012-01-20 19:48:29+00:00)
Title: Avoid returning from block in Dataset.single_record

Hi Jeremy,

I was debugging production issues recently and figured out that Dataset.single_record returns from block. Totaly understand the intent of it. Basically once we get the very first record we can return. However it has a side effect when you override Dataset.fetch_rows and using Model.first method to fetch only one row from DB. Here is snippet of code that overrides fetch_rows in order to handle caching. See comment there with details about the issues. Proposed code change fixes the problem. Run *rspec spec/core/dataset_spec.rb* and all tests passed

```ruby
def fetch_rows(sql)
element = cache_get(sql)
if element
(element.value || []).each {|row| yield row }
else
arr = []
super(sql) { |row| arr << row; yield row }
#!!!! Line below is never executed if Dataset.single_record returns from block
cache_put(sql, arr.empty? ? nil : arr)
end
end
```