Isn’t it nice when something just works? We are building Ruby on Rails apps on top of Oracle, so we’re using the Oracle Enhanced ActiveRecord adapter on top of the ruby-oci8 driver library.
The ActiveRecord adapter gives us a nice AR wrapper around our existing Oracle schema, which is great, but what about when I want to work with stored procedures or functions? Turns out the author of this adapter, Raimonds Simanovskis, has a gem just for this called ruby-plsql.
Include the gem in your Gemfile:
gem 'ruby-plsql'
Then, write an initializer that hooks it to your existing ActiveRecord connection (config/plsql.rb)
plsql.activerecord_class = ActiveRecord::Base
After that, calling procedure is easy. Oracle return types are automatically cast to ruby types. Oracle exceptions are raised as OciError, which contains a “code” and “sql” attribute. However, you can call a “message” method on that exception to get the full error output.
Here I call an Oracle procedure, idcard.nd_is_valid_pin using the plsql object provided in the gem:
ok_pin = plsql.idcard.nd_is_valid_pin( new_pin ) if ok_pin plsql.idcard.update_pin_pr( @info.ndid, params[:old_pin], pin ) else raise Errors::InvalidInput end rescue OCIError => e render json: { error: e.message }, status: :unprocessable_entity
That’s it! Nice and easy, and “rsims” is two for two.