2015-09-05 64 views
0

工作如果我让使用this documentation指定的unique_checks=0选项建立一个插入查询,然后运行它:ActiveRecord::Base.connection.execute(query)如何获得UNIQUE_CHECKS = 0红宝石活动记录

我得到一个错误(见下文)。但是,如果我复制包含在相同错误中的查询并在没有任何更改的情况下运行它,它就可以正常工作。所以查询起作用,只是关于活动记录如何运行而已。

我必须缺少一些选项才能使此工作查询通过ActiveRecord::Base.connection.execute任何帮助将不胜感激!

#Error Message 
=> #<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 'SET unique_checks=0; 
INSERT INTO listings (cat, sub_cat, name, address, city, st' at line 2: SET autocommit=0; 
SET unique_checks=0; 
INSERT INTO listings (cat, sub_cat, name, address, city, state, zip, phone, fax) VALUES("Accountants","Tax Return Preparation","H&R Block","1 Mohegan Sun Blvd","Uncasville","CT","06382","8603831139",NULL),("Accountants","Tax Return Preparation","Sigler Accounting Service","12 Case St - Ste 304","Norwich","CT","06360","8608877555","8608878927"); 
SET unique_checks=1; 
COMMIT;> 

#Working SQL Query I stripped out of above error message 
SET autocommit=0; 
SET unique_checks=0; 
INSERT INTO listings (cat, sub_cat, name, address, city, state, zip, phone, fax) VALUES("Accountants","Tax Return Preparation","H&R Block","1 Mohegan Sun Blvd","Uncasville","CT","06382","8603831139",NULL),("Accountants","Tax Return Preparation","Sigler Accounting Service","12 Case St - Ste 304","Norwich","CT","06360","8608877555","608878927"); 
SET unique_checks=1; 
COMMIT; 

回答

1

可惜ActiveRecord::Base不允许在单一execute多个查询。这是一项安全功能,您无法绕过此功能。