views:

392

answers:

2

Created a simple stored procedure in MySQL:

CREATE PROCEDURE `proc01`()
BEGIN
 SELECT * FROM users;
END

Start Rails console:

$ script/console
Loading development environment (Rails 2.3.5)
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x10343efa0>

Looks good. BUT, any more call to the same stored procedure via the existing connection will result in an Commands out of sync error:

>> 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 by a "reload!" command in the console:

>> reload!
Reloading...
=> true
>> User.connection.execute("CALL proc01")
=> #<Mysql::Result:0x1033f14d0>
>> 

How can I call MySQL stored procedure from Rails? Thanks~

+1  A: 

u can see this :

http://stackoverflow.com/questions/471517/is-it-possible-to-call-a-mysql-stored-procedure-from-ruby

and i think this link give a example :

http://guyharrison.typepad.com/guy_harrisons_blog/2006/04/mysql_stored_pr.html

Haim Evgi
Thanks. Those lead to a ticket: https://rails.lighthouseapp.com/projects/8994/tickets/3151-mysql-adapter-update-to-enable-use-of-stored-procedures So does it mean the current Rails version (without a patch) does not support MySQL stored procedure?
ohho
A: 

update

ohho