Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Relation#inspect memory failsafe #2623

Closed
wants to merge 2 commits into from

4 participants

@vrinek

Added a failsafe so that relation#inspect will not accidentally fill the memory with all the objects from the database on a faulty query.

http://stackoverflow.com/q/6859738/146272

activerecord/lib/active_record/relation.rb
@@ -431,8 +431,13 @@ module ActiveRecord
end
end
- def inspect
- to_a.inspect
+ def inspect(force = false)
+ c = count
+ if c > 100 and !force
@dasch
dasch added a note

I believe it is customary to use &&, not and, in the Rails codebase.

@vrinek
vrinek added a note

A quick grep shows that you are right, (editing... done)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@vrinek

There is a problem with this approach: calling count on an ActiveRecord::Relation mitigates any limit(n) that was previously set

@dmathieu
Collaborator

Inspect is meant to be used in tests and console only, not in a codebase.
Therefore I don't see any moment when we wouldn't want to have this set to true.
Moreover, if we're comparing a lot of objects in a test, the result will be biased.

-1

@vrinek

The problem only exists when in production one has a exception notifier that sends an email containing the backtrace + an inspection of the env variables back at the developers. I believe that filling the production machine's memory with a huge inspection string is not one of the admin's favorite things.

My solution might be a bit ugly but in development it served me and a couple of my colleagues well when a broken Arel query inspection started filling in more than 2GB of ram.

@vrinek vrinek closed this
@vrinek vrinek reopened this
@arunagw
Collaborator

What's happening to this PR guys! Are we still on this?? Or this can be closed??

@arunagw
Collaborator

Closing this for now. Please reopen this with more details.

Cheers,
Arun

@arunagw arunagw closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 22, 2011
  1. @vrinek

    Added a failsafe so that relation#inspect will not accidentally fill …

    vrinek authored
    …the memory with all the objects from the database on a faulty query. http://stackoverflow.com/q/6859738/146272
Commits on Aug 23, 2011
  1. @vrinek
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 2 deletions.
  1. +7 −2 activerecord/lib/active_record/relation.rb
View
9 activerecord/lib/active_record/relation.rb
@@ -431,8 +431,13 @@ def ==(other)
end
end
- def inspect
- to_a.inspect
+ def inspect(force = false)
+ c = count
+ if c > 100 && !force
+ "WARNING: #{c} #{@klass} objects would be inspected.\nUse .inspect(true) for an actual inspection."
+ else
+ to_a.inspect
+ end
end
def with_default_scope #:nodoc:
Something went wrong with that request. Please try again.