问题
I'd like my Rails app to run a raw sql command after it establishes the connection to the DB. In which file does that belong? One of the config/initializers?
回答1:
I use monkeypatching to force strict mode for MySQL, the same approach should also work in your case. This code belongs in an initializer.
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
For reference, here's the source code for Mysql2Adapter.
回答2:
I think You can write a rake filter, through which you can fire a query before each inbound call. You can read more about this here.
来源:https://stackoverflow.com/questions/29224331/run-raw-sql-in-rails-after-connecting-to-database