Fix for ActiveRecord::ConnectionNotEstablished with acts_as_votable gem (0.5.0) on Rails 3.0.19

Steve Robinson

1 min read

When we were building the app we faced a certain issue with the acts_as_votable gem (~> 0.5.0) with Rails ‘3.0.19’.
We got the following stack trace and we figured it had something to do with the ActiveRecord database connection not being present when the gem was being loaded. Take a look at the stack trace:
in `retrieve_connection’:
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
in `retrieve_connection’
/Users/—-/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.0.19/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection’

/Users/—/.rvm/gems/ruby-1.9.3-p327/gems/acts_as_votable-0.5.0/lib/acts_as_votable/vote.rb:3:in `
As we can see from the stack trace above, the issue is caused by line 16 in the ‘/lib/acts_as_votable/vote.rb’ class in the gem. After going through the GitHub repository of the gem ( This line is where the votable gem declares some scopes to fetch records.
scope :up, where(:vote_flag => true)
As you can see the scope is not inside a proc/lambda. Rails seems to be evaluating this statement and its verifying the attributes specified in this query. And for that it needs a database connection. The weird thing is that Rails makes a database connection before loading gems. Anyway, when we enclosed the scope in a block everything started working fine. The server started up and there was no problem using it.
scope :up, {where(:vote_flag => true)}
The latest release is 0.5.0. The authors have patched the master branch of the gem at Github for supporting Rails 4. And since Rails 4 makes it mandatory to provide callable blocks to be passed to scopes, they have enclosed the scope statements in a block. But they have not made a release yet and so for the time being if you are facing this issue you can fetch the gem from their master branch by using the following in your Gemfile.
gem 'acts_as_votable',:git => 'git://'
Hope this helps you.

Related posts:

One Reply to “Fix for ActiveRecord::ConnectionNotEstablished with acts_as_votable gem (0.5.0) on Rails…”

  1. Thanks – this helped me as I worked through an application that’s new to me. As I started to write rspec tests as a way to understand things, I ran into this same type of error — although the acts-as-votable gem isn’t used. Your post was spot on: some of the scopes were enclosed in lambda blocks, but some (including the one causing a problem) weren’t. Using blocks took care of it.
    Thanks for saving me hours and hours!

Leave a Reply

Your email address will not be published. Required fields are marked *