1) Create a class and let’s call it
Class CallStoredProcedures
your methods here …….
end
2) Create a class method within it
def self.fetch_db_records(proc_name_with_parameters)
ActiveRecord::Base.connection.execute(proc_name_with_parameters)
end
Then simply call it as
CallStoredProcedures.fetch_db_records(“exec procedure_name ‘args1′, ‘args2′, ‘and so on’ “)
Now you might say, that this will only execute a procedure what if I want to return records or values from stored procedures. Then I will prefer class methods and this is how I would do it,
class CallStoredProcedures < ActiveRecord::Base
def self.fetch_db_records(proc_name_with_parameters)
connection.select_all(proc_name_with_parameters)
end
def self.insert_update_delete_calculate(proc_name_with_parameters)
connection.execute(proc_name_with_parameters)
end
def self.fetch_val_from_sp(proc_name_with_parameters)
connection.select_value(proc_name_with_parameters)
end
end
And then simply call it like
CallStoredProcedures.method_name(“exec procedure_name ‘args1′, ‘args2′, ‘and so on’ “)
More information click