Invoking Oracle package function before ActiveRecord query executions

我只是一个虾纸丫 提交于 2019-12-11 10:03:03

问题


I'm migrating an existing app to Rails 5 and using Devise for authentication. Database is Oracle12c.

I'm trying to figure out how to execute the following sql on the rails connection whenever a user would query the database. This context is required for the schema triggers to function correctly and this context should reflect the username of the user that is executing the query.

begin 
   main.ENV_VAR.prc_set_context_value('UserContext', '#{current_user}'); 
end;

Is there some sort of ActiveRecord hook to use to whenever a user is querying? Is there a Devise hook to use? Do I override ActiveRecord::Base?

Thanks for any assistance.


回答1:


I don't know anything about Oracle, so adjust the execute command itself based on what makes sense. This should be in your ApplicationController or whatever base controller you do your devise authenticate_user! callback in, and make sure this comes after that callback:

before_action :authenticate_user!
before_action :set_user_context

protected
def set_user_context
  if current_user
    ApplicationRecord.connection.execute "main.ENV_VAR.prc_set_context_value('UserContext', '#{current_user.id}')"
  end
end

Note that I also switched your current_user to current_user.id.



来源:https://stackoverflow.com/questions/54833648/invoking-oracle-package-function-before-activerecord-query-executions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!