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