Skip to content

Instantly share code, notes, and snippets.

@dougcole
Created December 5, 2012 00:36
Show Gist options
  • Save dougcole/4210726 to your computer and use it in GitHub Desktop.
Save dougcole/4210726 to your computer and use it in GitHub Desktop.
Simple class for running an ActiveRecord query in a separate thread.
class ConcurrentQuery < BasicObject
def initialize(&block)
@query = ::Thread.new do
result = begin
block.call
ensure
::ActiveRecord::Base.connection_pool.release_connection
end
result
end
end
def method_missing(method_name, *args, &block)
@query.value.send(method_name, *args, &block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment