Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print PG Result Object in Rails Console

I am doing some basic SQL queries in my rails console. When I query my postgreSQL database, the result is a PG Result object as seen here:

#<PG::Result:0x007f26cac82010 status=PGRES_TUPLES_OK ntuples=1 nfields=1 cmd_tuples=1>

My query looks like the following:

sql = "SELECT COUNT (DISTINCT Business_id) FROM applications"
result = ActiveRecord::Base.connection.execute(sql)

I'm currently doing a basic COUNT query and I would like to see the count result.

How can I view the data inside of the PG Result object?

like image 648
Questifer Avatar asked Sep 15 '15 13:09

Questifer


2 Answers

result.values

will print a multidimensional array of the results

like image 175
DylanReile Avatar answered Nov 04 '22 19:11

DylanReile


The PGResult object has some special methods for retrieving the results, the 'getvalue' method returns the tuple n as a hash, so then you could do:

result.getvalue(0,0) 

More detailed information can be found here here:

http://deveiate.org/code/pg/PG/Result.html

like image 17
RichardAE Avatar answered Nov 04 '22 21:11

RichardAE