# Mod\_ruby postgres

**URL:** <https://www.ruby-forum.com/t/mod-ruby-postgres/74909>\
**Category:** Ruby\
**Created:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/mod-ruby-postgres/74909 "2006-11-07T11:01:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parragh\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@Parragh\_S](https://www.ruby-forum.com/u/Parragh_S)\
**Post date:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/mod-ruby-postgres/74909/1 "2006-11-07T11:01:12Z")

</div>

Hello,

I’m trying some code with mod\_ruby where I make a postgresql connection.  
The following works fine:

require ‘postgres’

def with\_db  
db = PGconn.connect(‘localhost’, 5432, ‘’, ‘’, ‘template1’,  
‘postgres’, ‘’)  
begin  
yield db  
ensure  
db.close  
end  
end

with\_db do |db|  
row = db.query(‘SELECT VERSION()’)  
puts row  
end

But when I alter the code extending the PGconn class to do the result  
set ceanup like this:

require ‘postgres’

class PGconn  
alias :exec\_no\_block :exec  
def exec(sql)  
res = exec\_no\_block(sql)  
return res unless block\_given?  
begin  
yield res  
ensure  
res.clear if res  
end  
end  
end

def with\_db  
db = PGconn.connect(‘localhost’, 5432, ‘’, ‘’, ‘template1’,  
‘postgres’, ‘’)  
begin  
yield db  
ensure  
db.close  
end  
end

with\_db do |db|  
db.exec(‘SELECT VERSION()’) do |res|  
puts res[0]  
end  
end

The latter code works from command line, making the same output as the  
first one, however, with mode\_ruby I get the following error in apache  
error.log:

[Sat Nov 04 13:28:08 2006] [error] mod\_ruby: error in ruby  
[Sat Nov 04 13:28:08 2006] [error] mod\_ruby: /var/www/ruby/e1b.rb:20:in  
`with_db': undefined method`connect’ for #Module:0xb6fb45b4::PGconn  
(NoMethodError)

Could someone please help me out with this? It might be an obvious fault  
in my code, but I cannot figure it out.

Thanks:  
Szab
