Skip to content

Instantly share code, notes, and snippets.

@compwron
Created October 1, 2014 22:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save compwron/48eed93cb9e0a86affb0 to your computer and use it in GitHub Desktop.
Save compwron/48eed93cb9e0a86affb0 to your computer and use it in GitHub Desktop.

Disclaimer: This took the work of lots of smart people to track down. I did not discover this on my own and do not deserve the credit for it.

Symptom:

(NoMethodError) "undefined method `fields' for nil:NilClass"

Observed while using rails v3.2.19 - https://github.com/rails/rails/blob/v3.2.19/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Easy fix:

# in database.yml (rails reads this)
defaults: &defaults
  adapter: postgresql
  prepared_statements: false

Fix for when it is still broken after the easy fix:

# This is metaprogramming. It can change all your app's behavior in unexpected ways. It is NOT threadsafe. 
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
	def exec_no_cache(sql, binds)
	          @connection.exec(sql, [])
	end
end

To test it:

<active_connection>.connection.instance_variable_get(:@connection).stubs(:async_exec).returns(nil)
# do some database operation in your app; see the "undefined method `fields' for nil:NilClass" error. Then apply above fix.

Usually manifests when: Something something postgres connection changes out from under you

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