运行 原始 SQL 在 Rails 连接到数据库后
Run Raw SQL in Rails after connecting to Database
我希望我的 Rails 应用在建立与数据库的连接后 运行 原始 sql 命令。那属于哪个文件? config/initializers?
之一
我认为您可以编写一个 rake 过滤器,通过它您可以在每次入站调用之前触发查询。您可以阅读有关此内容的更多信息 here。
我使用 monkeypatching 为 MySQL 强制使用严格模式,同样的方法也适用于您的情况。此代码属于初始化程序。
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
private
alias_method :configure_connection_without_autocommit, :configure_connection
def configure_connection
configure_connection_without_autocommit
execute "COMMAND_TO_ENABLE_AUTOCOMMIT"
end
end
供参考,这里是 source code for Mysql2Adapter。
我希望我的 Rails 应用在建立与数据库的连接后 运行 原始 sql 命令。那属于哪个文件? config/initializers?
之一我认为您可以编写一个 rake 过滤器,通过它您可以在每次入站调用之前触发查询。您可以阅读有关此内容的更多信息 here。
我使用 monkeypatching 为 MySQL 强制使用严格模式,同样的方法也适用于您的情况。此代码属于初始化程序。
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
private
alias_method :configure_connection_without_autocommit, :configure_connection
def configure_connection
configure_connection_without_autocommit
execute "COMMAND_TO_ENABLE_AUTOCOMMIT"
end
end
供参考,这里是 source code for Mysql2Adapter。