1

How do I view the contents of a returns Postgresql cursor?

I have a custom function in Postgresql that returns a cursor. In Rails the returned values looks like an array that contains:

#<Order:0x007f6b63a49220> and #<Order:0x007f6b63a49040>

when I try to view the contents of these using .first or [0] I get an error

edit:

index.html.erb

<p><% @results %>
<p><% @results.each do |k,v| %>
        <p><%= "key: #{k}" %></p>
        <p><%= "value: #{v}" %></p>
<% end %></p>

test_controller.erb

@results = Order.retention_data(shop_id)

YYYYMMDD__add_process_functionstToDb.rb

class AddProcessFunctionstToDb < ActiveRecord::Migration
  def up
    funcs = File.read(Rails.root.join('db', 'migrate', 'processfunctions.sql').to_s)
    execute funcs
  end
  def down
  end
end

processfunctions.sql

drop function if exists retention_data(ref refcursor, shopId integer);
create or replace function retention_data(ref refcursor, shopId integer) returns refcursor as $$
    begin
    open ref for
...
    close ref;
    return ref;
    end;
$$ language plpgsql;

edit2:

I tried

k.attribute_names

and it returned

"retention_data", "id"

they returned:

k.retention_data #> x (the name of the cursor)
k.id #> (nothing)

others:

@results.responds_to? #>undefined method `responds_to?' for #<ActiveRecord::Relation [#<Order id: nil>, #<Order id: nil>]>
3
  • how you get this Order?? (query) Commented Oct 9, 2016 at 22:34
  • added in the code Commented Oct 10, 2016 at 0:09
  • sorry, never use rails with sql functions. What get with puts k.inspect and puts k.class Commented Oct 10, 2016 at 3:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.