使用ActiveRecord在外部DB中执行功能/存储过程

时间:2015-08-22 22:10:34

标签: ruby-on-rails postgresql ruby-on-rails-4 activerecord

我正在尝试从我的Rails 4应用程序在外部PostgreSQL数据库中执行一个函数。我无法弄清楚如何做到这一点。

我连接到外部数据库,如下所示:

class Pgdb < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :pg_development
end

class Mytable < Pgdb
  self.table_name = 'mytable'
end

我可以像这样查询Mytable

@info = Mytable.where("name LIKE ? AND amount > 0", "%#{params[:name]}%")

我无法弄清楚如何执行存储过程,因为它不是table。即使我尝试设置self.table_name = my_stored_procedure并查询@info = Mytable.connection.execute("execute my_stored_procedure('#{params[:name]}')")。这给出了错误:ERROR: prepared statement "my_stored_procedure" does not exist

我也尝试过:

@info = Pgdb.connection.execute("execute my_stored_procedure('#{params[:name]}')")
ERROR> PG::InvalidSqlStatementName: ERROR: prepared statement "my_stored_procedure" does not exist : execute agg_backs('JOHNSON, WALTER, PORTRAIT')


@info = Pgdb.execute("execute my_stored_procedure('#{params[:name]}')")
ERROR> undefined method `execute' for Pgdb(abstract):Class

如何执行存储过程?

0 个答案:

没有答案