Simple stored procedure in MySQL:
CREATE PROCEDURE `proc01`()
BEGIN
SELECT * FROM users;
END
Launches the Rails console:
$ script/console
Loading development environment (Rails 2.3.5)
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x10343efa0>
Looks nice. BUT, calling the same stored procedure through an existing connection will result in an error. Commands are not synchronized :
>> 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
The error can be cleared using "reboot!". commands in the console:
>> reload!
Reloading...
=> true
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x1033f14d0>
>>
How can I call a MySQL stored procedure from Rails?
source
share