Re RoR Equivalence of SPROC and UDF
Dear Bryan,
Thank you for your answer.
I am so new to RoR that I know too little of RoR to appreciate the full
significance of your answer. I havenâ??t even finished reading the book
â??Programming Rubyâ?
The reason why I ask about the SPROC & UDF is a bit like a Chinese
beginner learning to speak English who first thinks in Chinese, mentally
translates it into English, and eventually speaks in English. This is
just a transition period. When he is more fluent, he will think and
speak in English.
My early exposure to web application was Asp.Net 2002 from a book
written for VB.Net and SQL 2000, where the author talked about
Data-Driven web-application.
So I built some web-application by first creating a database and then a
bunch of Stored Proc & UDF and used ADO.Net to run the SPROC.
One of the modules was to automatically arrange room-mates for
over-night event participants subject to some pre-defined criteria, such
as single participants can only room with the same sex; participants are
grouped accordingly to their sponsorsâ?? groups, meal preferences, etc.
To accomplish the room-mate pairings, I wrote a few stored procedures
with the first one calling on the subsequent SPROC & UDF. This is
probably not a good way but that was what I knew how.
In ADO.Net I can choose to invoke the Stored Procedure by using the name
of the first SPROC or I can also choose to write the whole sets of SPROC
into one long SQL statement in VB.Net and use ADO.Net to run it.
From your example of â??ActiveRecord::Base.connection.execute(sql)â?, I
guess I can use Ruby to write the set of SPROC into one sql statement
and plug it into a Model function somewhere.
Am I right to guess that there is no way to run the SPROC in RoR by
using the name of the SPROC? If there is a way, then what is the syntax?
Regards,
Ling
Bryan D. wrote:
I struggle to see the advantage of using stored procedures in Ruby on
Rails. Part of the appeal of RoR is getting to use ActiveRecord: you
don’t have to think about the database as a database because it takes
care of all the work for you. Where would a SPROC fit into this
methodology?
Basically, instead of writing a stored procedure, write a model
function. It keeps all of your app in one place and in one language. I
don’t know if this qualifies as DRY, but it sure is “don’t spread
yourself out.”
If you REALLY need to use them for some reason, just use direct database
access to invoke the procedure in SQL.
ActiveRecord::Base.connection.execute(sql) will get you an array result.