Một quy trình đơn giản lưu trữ trong MySQL:Làm thế nào để gọi thủ tục lưu trữ MySQL từ Rails?
CREATE PROCEDURE `proc01`()
BEGIN
SELECT * FROM users;
END
Bắt đầu Rails console:
$ script/console
Loading development environment (Rails 2.3.5)
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x10343efa0>
Trông tốt. NHƯNG, bất kỳ cuộc gọi hơn với cùng một thủ tục lưu trữ thông qua kết nối hiện tại sẽ dẫn đến một Commands đồng bộ lỗi:
>> 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
Các lỗi có thể được xóa bởi một "reload!" lệnh trong bảng điều khiển:
>> reload!
Reloading...
=> true
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x1033f14d0>
>>
Làm cách nào để gọi thủ tục lưu sẵn MySQL từ Rails?
Tôi đăng một số thông tin tiếp theo tại: http://www.ruby-forum.com/topic/193977#899074 (do SO không hỗ trợ danh sách mã trong nhận xét) Có thể" kết nối! nếu! hoạt động? "có thể là một sửa chữa.Không chắc đó có phải là câu trả lời cho câu hỏi này hay không. – ohho
Tóm tắt trong nhật ký web của tôi: http://ho.race.hk/blog/?p=231 – ohho