How rails calls the MySQL Stored Procedure

Source: Internet
Author: User

There is a need for a recent project to use views and stored procedures in ror Development

Because we want to use the view, and the rails migration file has the create_table method, we have to find whether to create a view in the migration file (create_view method). By searching for the rails2.3.5 API, we have not found a method.
Because the project needs to involve a lot of computing, in terms of performance, we are ready to use the stored procedure. By looking for information about rails calling the MySQL stored procedure, we can conclude that: the stored procedure must be manually created on MySQL. In rails, you can only call the stored procedure that returns a single result set. The related code is as follows:
Return single result set:
Create a stored procedure
Max_grade ()
Create definer = 'root' @ 'localhost' procedure 'max _ case '()
Begin
Select ID from students where grade_point_average = 4;
End;

Rails call:
Def procedure
SQL = activerecord: Base. Connection ();
@ Result = SQL. select_value ('call max_grade ()');

Respond_to do | format |
Format.html
Format. xml {render: XML =>@ result}
End
End
The code in the procedure.html. ERB file is <% = @ result %>, and the page displays 2

Return multiple result sets:

Create a stored procedure
Max_grade ()
Create definer = 'root' @ 'localhost' procedure 'max _ case '()
Begin
Select * from students where grade_point_average = 4;
End;

Rails call:

Def Edit
@ Student = student. find_by_ SQL ('call max_grade ()');
End

The error "MYSQL: Error: commands out of sync; you can't run this command now: Show fields from 'students'" is displayed when you enter the edit.html. ERB page '"

No solution was found for finding some materials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.