When a controller action blows up in development, we usually get a backtrace with some useful info. When the same happens in production, we get it via email and display a 404 to the visitor. Either way we have some kind of error handling that inspects the instance and its variables.
Now when an incomplete Arel query gets inspected, it loads up a LOT of objects. Here is an example:
def index
@users = User.order("name asc")
if params[:query]
@users = @users.where("username like ?", "%{params[:query]}%")
else
@users = @users.limit(20)
end
end
When something inside the conditional blows up, the @users variable gets stuck with User.order("name asc") and when inspected, it loads up all the users from the database filling up the RAM.
Is there a way to avoid this from happening?
In the end, I hijacked the #inspect method of ActiveRecord::Relation like so: https://github.com/rails/rails/pull/2623