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