Friday, March 14, 2014

Fetch records from sql query - Ruby On Rails - Oci8 cursor

Let's say we need to fetch the records from this sql query in rails.
  
s = ActiveRecord::Base.connection.execute("select id, email from users" )

The output will be something like
  
  < OCI8::Cursor:0xf73ac30 >

which is nothing but a cursor object. But how to read the records from this cursor?.
The code to fetch the record is
  
while r = s.fetch_hash
   puts s
end
The output will be
  
{"ID"=>10005, "EMAIL"=>"2@gmail.com"}
{"ID"=>10006, "EMAIL"=>"3@gmail.com"}
{"ID"=>10002, "EMAIL"=>"1@gmail.com"}