Module: ActiveRecord::CursorExtensions

Extended by:
ActiveSupport::Concern
Included in:
Relation
Defined in:
lib/active_record/postgresql_cursors/cursors.rb

Instance Method Summary collapse

Instance Method Details

#cursor(*args) ⇒ Object



31
32
33
# File 'lib/active_record/postgresql_cursors/cursors.rb', line 31

def cursor(*args)
  find_with_cursors('cursor', *args)
end

#find_with_cursors(*args) ⇒ Object

Override ActiveRecord::Base#find to allow for cursors in PostgreSQL. To use a cursor, set the first argument of find to :cursor. A PostgreSQLCursor object will be returned, which can then be used as an Enumerable to loop through the results.

By default, cursor names are generated automatically using “cursor_#rand”, where rand is a big ol’ random number that is pretty unlikely to clash if you’re using nested cursors. Alternatively, you can supply a specific cursor name by supplying a :cursor_name option.



21
22
23
24
25
26
27
28
29
# File 'lib/active_record/postgresql_cursors/cursors.rb', line 21

def find_with_cursors(*args)
  if args.first.to_s == 'cursor'
    options = args.extract_options!
    cursor_name = options.delete(:cursor_name)
    find_cursor(cursor_name, options)
  else
    find_without_cursors(*args)
  end
end