在执行ActiveRecord查询之前调用Oracle软件包功能 [英] Invoking Oracle package function before ActiveRecord query executions

查看:77
本文介绍了在执行ActiveRecord查询之前调用Oracle软件包功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将现有应用程序迁移到Rails 5,并使用Devise进行身份验证.数据库是Oracle12c.

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

我试图弄清楚每当用户查询数据库时如何在rails连接上执行以下sql.该上下文是架构触发器正确运行所必需的,并且该上下文应反映正在执行查询的用户的用户名.

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;

用户查询时是否有某种ActiveRecord挂钩可用于?是否有可以使用的Devise挂钩?我要重写ActiveRecord :: Base吗?

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?

感谢您的帮助.

推荐答案

我对Oracle一无所知,因此请根据实际情况调整执行命令本身.这应该在您的ApplicationController或您进行设计authenticate_user!回调的任何基本控制器中,并确保它在该回调之后出现:

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

请注意,我还将您的current_user切换为current_user.id.

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

这篇关于在执行ActiveRecord查询之前调用Oracle软件包功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆