ActiveRecord在Rails 3中生成SQL

时间:2021-08-06 00:31:35

I'm working on a project which requires talking to two different databases: a MySQL one for persistent data and an experimental in-memory database for real-time data.

我正在做一个项目,这个项目需要与两个不同的数据库进行对话:一个是用于持久数据的MySQL数据库,另一个是用于实时数据的实验性内存数据库。

It would make my life a bit easier if there was a way to access and use the SQL Query generated by the ActiveRecord finders.

如果有一种方法可以访问和使用ActiveRecord查找程序生成的SQL查询,那么我的生活就会轻松一些。


I know there have been other questions along these lines but the answers they had seemed to suggest logging the SQL to STDOUT and were not for Rails 3

我知道在这些方面还有其他问题,但是他们的回答似乎建议将SQL记录到STDOUT,而不是Rails 3。

1 个解决方案

#1


5  

Model.all executes the query and you are trying to call to_sql on an Array.

模型。所有的操作都执行这个查询,您正在尝试调用一个数组中的to_sql。

Do this

这样做

Model.scoped.to_sql

#1


5  

Model.all executes the query and you are trying to call to_sql on an Array.

模型。所有的操作都执行这个查询,您正在尝试调用一个数组中的to_sql。

Do this

这样做

Model.scoped.to_sql