从 Rails 4 应用程序调用大量 SQL [英] Invoking a large set of SQL from a Rails 4 application
问题描述
我有一个 Rails 4 应用程序,我将它与 sidekiq
结合使用来运行异步作业.我通常在 Rails 应用程序之外运行的工作之一是大量复杂的 SQL 查询,它们无法真正由 ActiveRecord 建模.这组 SQL 查询与我的 Rails 应用程序之间的联系是它应该在调用我的控制器操作之一时执行.
I have a Rails 4 application that I use in conjunction with sidekiq
to run asynchronous jobs. One of the jobs I normally run outside of my Rails application is a large set of complex SQL queries that cannot really be modeled by ActiveRecord. The connection this set of SQL queries has with my Rails app is that it should be executed anytime one of my controller actions is invoked.
理想情况下,我会将 Rails 应用程序中的作业排在控制器中,以便 Sidekiq 继续运行查询.现在它们存储在一个外部文件中,我不完全确定让 Rails 运行上述 SQL 的最佳方法是什么.
Ideally, I'd queue a job from my Rails application within the controller for Sidekiq to go ahead and run the queries. Right now they're stored in an external file, and I'm not entirely sure what the best way is to have Rails run the said SQL.
感谢任何解决方案.
推荐答案
我同意 Sharagoz,如果你只是需要运行一个特定的查询,最好的方法是将查询字符串直接发送到连接中,例如:>
I agree with Sharagoz, if you just need to run a specific query, the best way is to send the query string directly into the connection, like:
ActiveRecord::Base.connection.execute(File.read("myquery.sql"))
如果查询不是静态的并且您必须编写它,我会使用 Arel,它已经存在于 Rails 4.x 中:
If the query is not static and you have to compose it, I would use Arel, it's already present in Rails 4.x:
https://github.com/rails/arel
这篇关于从 Rails 4 应用程序调用大量 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!