2012-10-22 39 views
0

我尝试创建具有中间结果specialy报表的表。 我在做什么是:ActiveRecord执行原始sql

ActiveRecord::Base.connection.execute(sql_query) 

sql_query是:

SET SQL_SAFE_UPDATES=0; 

     DROP TABLE IF EXISTS _by_people_daily; 

     CREATE TEMPORARY TABLE _by_people_daily 
     AS 
     SELECT #{date_int} AS date_int, 
      memberships.user_id AS user_id, 
      ci.community_id, 
      ci.content_id, 
      contents.composite_type AS content_type, 

      COUNT(views.id) AS views, 
      COUNT(comments.id) AS comments, 
      COUNT(shares.id) AS shares, 
      COUNT(likes.id) AS likes, 
      COUNT(uploads.id) AS uploads 
     FROM community_items AS ci 
     JOIN memberships ON memberships.community_id = ci.community_id 
     JOIN contents ON ci.content_id = contents.id 
     JOIN (SELECT '#{day_begin}' as start_date, '#{day_end}' as end_date) AS dates 
     LEFT JOIN contents AS uploads 
       ON uploads.id = ci.content_id 
       AND uploads.user_id = memberships.user_id 
       AND uploads.created_at BETWEEN dates.start_date AND dates.end_date 
     LEFT JOIN comments 
       ON comments.content_id = ci.content_id 
       AND comments.user_id = memberships.user_id 
       AND comments.created_at BETWEEN dates.start_date AND dates.end_date 
     LEFT JOIN shares 
       ON shares.content_id = ci.content_id 
       AND shares.user_id = memberships.user_id 
       AND shares.created_at BETWEEN dates.start_date AND dates.end_date 
     LEFT JOIN likes 
       ON likes.content_id = ci.content_id 
       AND likes.user_id = memberships.user_id 
       AND likes.created_at BETWEEN dates.start_date AND dates.end_date 
     LEFT JOIN views 
       ON views.viewable_id = ci.content_id 
       AND views.viewable_type = 'Content' 
       AND views.user_id = memberships.user_id 
       AND views.created_at BETWEEN dates.start_date AND dates.end_date 
     WHERE COALESCE(views.id, comments.id, shares.id, likes.id, uploads.id) IS NOT NULL 
     #{ " AND users.company_id = #{@company.id} " if @company } 
     GROUP BY memberships.user_id, ci.community_id, ci.content_id, contents.composite_type ; 

     DELETE FROM intermediate_by_people WHERE date_int = #{date_int} ; 

     INSERT INTO intermediate_by_people 
      (date_int, user_id, community_id, content_id, content_type, views, comments, shares, uploads, likes) 
     SELECT date_int, user_id, community_id, content_id, content_type, views, comments, shares, uploads, likes 
     FROM _by_people_daily ; 

每次

ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP TABLE IF EXISTS _by_people_daily; 

但是当我通过查询到mysql客户端它正常工作,我得到错误。

我做错了什么,以及如何执行与ActiveRecord的

回答

3

的ActiveRecord的MySQL连接只能在默认情况下执行一个语句。要执行多个语句,创建连接时需要将CLIENT_MULTI_STATEMENTS(65536)作为选项传递。该代码可能是这样的:

ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket, 65536], config) 

你可以在你的mysql宝石ConnectionAdapters::MysqlAdapter.new,并覆盖方法config/initializersHere是一个详细的指南。

+0

真棒谢谢! – Vladimir

+0

链接到详细指南已腐烂。你可以请更新或删除它? – Envek

+0

今天,您可以将'MULTI_STATEMENTS'数组传递给'database.yml'中的'flags'选项。请参阅http://stackoverflow.com/a/43633321/338859 – Envek

1

你尝试逃避查询几个SQL语句?

DROP TABLE IF EXISTS `_by_people_daily` 

以下划线开头的表格对我来说看起来很可疑。

更新:

是什么我已经通过谷歌发现:http://www.seanr.ca/tech/?p=75

+0

我重命名表并尝试注释掉它无助的一些表格。 – Vladimir

+0

您的链接也关于CLIENT_MULTI_STATEMENTS,谢谢 – Vladimir