MySQL中的简单存储过程:
CREATE PROCEDURE `proc01`()
BEGIN
SELECT * FROM users;
END
启动Rails控制台:
$ script/console
Loading development environment (Rails 2.3.5)
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x10343efa0>
看起来不错。但是,如果通过现有连接再次调用相同的存储过程,将导致命令不同步错误:
>> User.connection.execute("CALL proc01")
ActiveRecord::StatementInvalid: Mysql::Error: Commands out of sync; you can't run this command now: CALL proc01
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:in `execute'
from (irb):2
错误可以通过控制台中的“reload!”命令来清除:
>> reload!
Reloading...
=> true
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x1033f14d0>
>>
我该如何从Rails中调用MySQL存储过程?