如何在 ruby​​ on rails 中调用存储过程? [英] how to call stored procedure in ruby on rails?

查看:29
本文介绍了如何在 ruby​​ on rails 中调用存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ROR 的新手.当我单击 VIEW 中的提交按钮时,我想调用存储过程进行处理.

 型号:-------类 Pro::DataImport <%= file_field_tag ':file_name' %>

<div class="actions"><%= submit_tag '导入文件'%>

<%结束%>存储过程---------------ALTER PROCEDURE "DBA"."my_enr_test"(file_name long varchar)开始INSERT INTO DBA.pro_data_imports(file_name) values(file_name);结尾

提前致谢..请帮助我.我想从上传按钮获取文件路径并将其存储到数据库列 file_name 中.如何执行提交按钮的存储过程.请帮帮我!!

解决方案

如果您使用的是 ActiveRecord SQLServer Adapter,请查看:

http://rubydoc.info/gems/activerecord-sqlserver-adapter/3.2.9/ActiveRecord/ConnectionAdapters/Sqlserver/DatabaseStatements:execute_procedure

在你的代码中做这样的事情

class Pro::DataImport 

每一个普通的 SQL 查询都会被转换成一个存储过程来执行.这就是 ActiveRecord 的 SQL Server 适配器的工作方式.所以你只需要为数据库中定义的永久存储过程担心这个.

I am new to ROR. I want to call the Stored Procedure to process when I click the submit button in the VIEW.

    Model:
    -------

    class Pro::DataImport < ActiveRecord::Base
      attr_accessible :file_name, :process_name, :updated_by, :validates

    end

    Controller:
    -----------------

    class Pro::DataImportsController < ApplicationController
       before_filter :authenticate_user!
      layout "layouts/enr/energy_master"

      def index
       @pro_data_imports = Pro::DataImport.all 
      end

      def new
        @pro_data_import = Pro::DataImport.new
      end

    end

    View
    ----------

     <%= form_for @pro_data_import do %>

  <div class="field">
    Browse the file to upload:<br />
    <%= file_field_tag ':file_name' %>
  </div>

  <div class="actions">
    <%= submit_tag 'Import File' %>
  </div>
<% end %>


    Stored Proc
    ---------------

    ALTER PROCEDURE "DBA"."my_enr_test"(file_name long varchar)
    BEGIN
        INSERT INTO DBA.pro_data_imports(file_name) values(file_name);
    END

Thanks in Advance.. Please Help me. I want to get the filepath from the upload button and store into the database column file_name. How to execute the store procedure for the submit button. Please help me!!

解决方案

if you are using the ActiveRecord SQLServer Adapter, checkout:

http://rubydoc.info/gems/activerecord-sqlserver-adapter/3.2.9/ActiveRecord/ConnectionAdapters/Sqlserver/DatabaseStatements:execute_procedure

do something like this in your code

class Pro::DataImport < ActiveRecord::Base
  def self.update(user)
    self.execute_procedure("Stored Procedure Name", arg1, arg2)
  end
end

Every normal SQL query is converted to a stored procedure to be executed. That is how the SQL Server adapter for ActiveRecord works. So you only have to worry about this for permanent stored procedures defined in the database.

这篇关于如何在 ruby​​ on rails 中调用存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆