Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails and Oracle: execute sql statement and get all data

I have a rather complex sql statement that is created on the fly. But the same problem exists for a simple query, so I use this one as an example.

I have a Feature model. I can call Feature.count --> 4

But if I try to get all Feature ids using ActiveRecord::Base.connection.execute("SELECT ID from features") the result is a OCI8::Cursor object. I do not know how to get the data out of it.

If I try ActiveRecord::Base.connection.execute("SELECT ID from features").fetch I get [1]. With xyz.fetch_hash I get {"ID" => 1}.

I would like to have all IDs. I just switched from PostgreSQL to Oracle.

Using PostgreSQL, I got all data using ActiveRecord::Base.connection.execute("SELECT ID from features").to_a, but to_a does not exist using the activerecord-oracle_enhanced-adapter gem (*** NoMethodError Exception: undefined method 'to_a' for #<OCI8::Cursor:0x00000007028aa8>).

I am using Ruby 2.0 and Rails 4. The connection to the Oracle database is working, except my manually created statement.

like image 591
Bjoernsen Avatar asked Sep 19 '13 09:09

Bjoernsen


1 Answers

I found a solution.

Old: ActiveRecord::Base.connection.execute("SELECT ID from features")

New: ActiveRecord::Base.connection.exec_query("SELECT ID from features").to_a

Maybe someone have the same problem.

like image 68
Bjoernsen Avatar answered Nov 18 '22 02:11

Bjoernsen