rubyruby-on-rails-4rails-geocoderrubinius

undefined method `to_sym' on nil:NilClass on rubysl/net/http/http.rb caused by Rails Geocoder


So I've working on a website on Ruby on Rails that uses the Geocoder gem.

Here is my implementation:

def User < ActiveRecord::Base    
    before_save :geocode
    geocoded_by :address do |usr, geocode_results|
    if geo = geocode_results.first
       usr.latitude  = geo.latitude
       usr.longitude = geo.longitude
       usr.geocode_address = geo.address
    else
      usr.latitude  = nil
      usr.longitude = nil
      usr.geocode_address = nil
    end
end

And on production initially it works... But in 2-3 days I'll get an error saying undefined method to_sym on nil:NilClass. on any controller action that calls @user.save

I investigated further, and here is the stack trace:

Stacktrace (most recent call first):   
kernel/delta/kernel.rb:78:in `to_sym (method_missing)'
rubysl/net/http/http.rb:576:in `start'
   key = $1.to_sym
kernel/common/enumerable.rb:217:in `grep'
kernel/bootstrap/array.rb:76:in `each'
kernel/common/enumerable.rb:213:in `grep'
rubysl/net/http/http.rb:575:in `start'
    http.methods.grep(/\A(\w+)=\z/) do |meth|
geocoder/lookups/base.rb:268:in `make_api_request'
    http_client.start(uri.host, uri.port, use_ssl: use_ssl?) do |client|
rubysl/timeout/timeout.rb:149:in `timeout'
    yield sec
rubysl/timeout/timeout.rb:168:in `timeout'
     Timeout.timeout(n, e, &block)
geocoder/lookups/base.rb:266:in `make_api_request'
    timeout(configuration.timeout) do
geocoder/lookups/base.rb:218:in `fetch_raw_data'
    response = make_api_request(query)
geocoder/lookups/base.rb:169:in `fetch_data'
    parse_raw_data fetch_raw_data(query)
geocoder/lookups/google.rb:28:in `results'
    return [] unless doc = fetch_data(query)
geocoder/lookups/base.rb:47:in `search'
    results(query).map{ |r|
geocoder/query.rb:11:in `execute'
    lookup.search(text, options)
geocoder.rb:21:in `search'
    query.blank? ? [] : query.execute
geocoder/stores/base.rb:111:in `do_lookup' 
    results = Geocoder.search(query, query_options)
geocoder/stores/active_record.rb:259:in `geocode'
    do_lookup(false) do |o,rs|   active_support/callbacks.rb:424:in `make_lambda'
    lambda { |target, _, &blk| target.send filter, &blk }

It turns out that this is the culprit on the to_sym for nil:NilClass error - it seems that the grep isn't finding anything?

The Net:HTTP is being called by Rails Geocoder here.

I suspect its an rbx-2.2.1 issue, so I'm gonna try switching to ruby-2.2.1 and see if it happens in the next couple days. Has this ever happened to anyone?

Edit: If I restart my rails server thread the problem goes away, but will come back in ~1-2 days

Edit: this is the user_controller that caused the issue

respond_to do |format| #line 169
    @id = @user.id
    @user.update(user_params) #line 171
    if params[:user][:redirect_to_index].present? or @user.chef?
        format.html { redirect_to root_path }
        format.json { render :show, status: :ok, location: @user }
    else
        format.html { redirect_to @user }
        format.json { render :show, status: :ok, location: @user }
    end
end

Solution

  • I fixed the issue by switching from rbx-2.2.1 to ruby-2.2.1. It seems to be caused by some bug between geocoder and rbx-2.2.1 (I think).