如何对每个数据库连接执行查询 [英] How to execute a query per db connection
本文介绍了如何对每个数据库连接执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在执行以下操作,但效率不高,因为它是在每次操作之前调用它
I'm currently doing the following which works but is inefficient since it's calling it before every action
class ApplicationController < ActionController::Base
before_action :set_intervalstyle
private
def set_intervalstyle
ActiveRecord::Base.connection.exec_query("SET intervalstyle = iso_8601", "SCHEMA")
end
end
我注意到此处他们正在为每个连接注册此命令
I noticed here that they're registering this command per connection
alias_method :configure_connection_without_interval, :configure_connection
define_method :configure_connection do
configure_connection_without_interval
execute('SET intervalstyle = iso_8601', 'SCHEMA')
end
有人可以帮我弄清楚如何将我的before_action转换成这样的东西吗?也许作为初始化器?我不确定从哪里开始
Could someone help me figure out how to convert my before_action into something like this? Maybe as an initializer? I'm not sure where to start
推荐答案
不确定这是否是个好主意,但到目前为止,它仍然有效并且没有任何副作用
Not sure if this is a good idea but so far this works and hasn't had any side effects
config/initializers/set_intervalstyle.rb
config/initializers/set_intervalstyle.rb
require 'active_record/connection_adapters/postgresql_adapter'
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
alias_method :configure_connection_without_interval, :configure_connection
def configure_connection
configure_connection_without_interval
execute('SET intervalstyle = iso_8601', 'SCHEMA')
end
end
这篇关于如何对每个数据库连接执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文