_jkb6w
Last Updated: February 25, 2016
·
3.494K
· yannick
F17e757902ab6a18cba1bacaa976eb0f

Stored Procedures and ActiveRecord

There is no easy way to execute a stored procedure. For that I made a small service on top of ActiveRecord that returns OpenStruct

class StoredProcedureService

  def self.instance
    @instance ||= StoredProcedureService.new
  end

  def execute(name, *args)
    results = []
    begin
      connection.execute("CALL #{name}(#{args.join(',')})").each(as: :hash, symbolize_keys: true) do |row|
        results << OpenStruct.new(row)
      end
    ensure
      connection.close
    end
    results
  end

  def connection
    ActiveRecord::Base.connection
  end
end

Easiest to see it on a gist! https://gist.github.com/3714101

Say Thanks
Respond

2 Responses
Add your response

3009
17e687280e2ddc751e52329f1ca63730

Good

over 1 year ago ·
3010
F17e757902ab6a18cba1bacaa976eb0f

@ohmaneel Thanks

over 1 year ago ·