A D Vishnu Prasad

CID - Cloud Infrastructure Desinger

Fetch Records From Sql Query - Ruby on Rails - Oci8 Cursor

There might be time when you can’t use ActiveRecord finders but have to write custom queries to fetch the records.

Below example is to fetch records using oci8 adapter.(i.e If you are using oracle db)

1
users = ActiveRecord::Base.connection.execute("select id, email from users" )

The output will be

1
<OCI8::Cursor:0xf73ac30>

which is nothing but a cursor object. But how to read the records from this cursor?.

1
2
3
while r = users.fetch_hash
   puts users
end

The output will be

1
2
3
{"ID"=>10005, "EMAIL"=>"2@gmail.com"}
{"ID"=>10006, "EMAIL"=>"3@gmail.com"}
{"ID"=>10002, "EMAIL"=>"1@gmail.com"}

Comments