geocoder
geocoder copied to clipboard
undefined method `geocoded_by' for XXXXXX
I installed the gem and it works pretty good in the browser. No issues.
When I run the console though I start having problems.
if I try to query my model from the console i get:
NoMethodError: undefined method `geocoded_by' for Tour (call 'Tour.connection' to establish a connection):Class
Is Tour
an ActiveRecord model?
Yes it is
class Tour < ApplicationRecord
has_many :events
#This validates presence of title, and makes sure that the length is not more than 140 words
validates :title, presence: true, length: {maximum: 140}
#This validates presence of body
validates :body, presence: true
validates :locality, presence: true
validates_uniqueness_of :slug
mount_uploader :cover, CoverUploader
validates_presence_of :slug
geocoded_by :locality # can also be an IP address
after_validation :geocode # auto-fetch coordinates
def self.search(query)
where("title like ? OR locality like ?", "%#{query}%", "%#{query}%")
end
def to_param
slug
end
end
I am experiencing this same issue with reverse_geocoded_by
, funny thing is, it was working fine, then after reloading the app code through reload!
in the console, now I can't access anything from the model that includes the reverse_geocoded_by
snippet.
model in question:
# app/models/entry_port.rb
class EntryPort < ApplicationRecord
validates :port_number, presence: true, uniqueness: { case_sensitive: false }
has_many :crossing_time_updates, dependent: :destroy
reverse_geocoded_by :latitude, :longitude
after_validation :reverse_geocode, if: ->(obj) { obj.latitude.present? and obj.longitude.present? } # auto-fetch address
end
in rails console:
[6] pry(main)> EntryPort.new
NoMethodError: undefined method `reverse_geocoded_by' for EntryPort (call 'EntryPort.connection' to establish a connection):Class
from /Users/aantillon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.1.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
[7] pry(main)> EntryPort.connection
NoMethodError: undefined method `reverse_geocoded_by' for EntryPort (call 'EntryPort.connection' to establish a connection):Class
from /Users/aantillon/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.1.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
Any help or tips would be appreciated
Hmmm my instinct is to this that this is a similar issue to: https://github.com/alexreisner/geocoder/issues/966
But documentation seems to indicate that ApplicationRecord is inheriting from ActiveRecord::Base?
If this is blocking anyone, you could switch back to using < ActiveRecord::Base
for now.
Any updates on this? Does switching to ActiveRecord::Base
solve the issue?
I've found that react-rails
conflicts with this - interestingly, placing the react-rails
gem before geocoder
in my Gemfile causes a NoMethodError
, while everything works fine when I place it after geocoder
.
This is on Rails 4.2, Ruby 2.3.5. My models inherit from ActiveRecord::Base
. Apologies if this is unrelated, but it sounds awfully similar to some of what's happening here - hoping this might shed additional light on what's going on.
@ATMartin thanks for that info! Very interesting. @antillas21 @giovapanasiti do either of you have react-rails
in your Gemfile
, before Geocoder? Regardless, we should look into this and figure out what's going on.
@alexreisner well, I don't have react-rails
in my Gemfile, my project is an API server and it is as bare-bones as possible.
I'm thinking the fix for #1318 may have fixed this too. Does installing the gem from current HEAD solve this for anyone?
Installing from head doesn't fix this for me
I've found that
react-rails
conflicts with this - interestingly, placing thereact-rails
gem beforegeocoder
in my Gemfile causes aNoMethodError
, while everything works fine when I place it aftergeocoder
.This is on Rails 4.2, Ruby 2.3.5. My models inherit from
ActiveRecord::Base
. Apologies if this is unrelated, but it sounds awfully similar to some of what's happening here - hoping this might shed additional light on what's going on.
This is still true in Rails 7.x+ with Ruby 3.x+. 👍