Search code examples
mysqlruby-on-railsruby-on-rails-4rails-activerecordmysql-5.5

Calling MySQL stored procedure in ROR 4


There are few example out there but non of them are very clarified (or on old version).

I want to call MySQL procedure and check the return status (in rails 4.2). The most common method I saw is to call result = ActiveRecord::Base.connection.execute("call example_proc()"), but in some places people wrote there is prepared method result = ActiveRecord::Base.connection.execute_procedure("Stored Procedure Name", arg1, arg2) (however it didn't compiled).

So what is the correct way to call and get the status for MySQL procedure?

Edit:

And how to send parameters safly, where the first parameter is integer, second string and third boolean?


Solution

  • Rails 4 ActiveRecord::Base doesn't support execute_procedure method, though result = ActiveRecord::Base.connection still works. ie

    result = ActiveRecord::Base.connection.execute("call example_proc('#{arg1}','#{arg2}')")