Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/json_in_batches/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object

:nodoc:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/json_in_batches/core_ext.rb', line 3

def as_json(options = nil) #:nodoc:
  # encode in batches only if there's no limit and order instructions,
  # because find_each ignores them
  if self.limit_value.nil? && self.orders.empty?
    # use encoder as a proxy to call as_json on all elements, to protect from circular references
    encoder = options && options[:encoder] || ActiveSupport::JSON::Encoding::Encoder.new(options)

    jsonified = []
    find_each { |v| jsonified << encoder.as_json(v, options) }
    jsonified
  else
    to_a.as_json(options)
  end
end