alexreisner / geocoder

Complete Ruby geocoding solution.
http://www.rubygeocoder.com
MIT License
6.33k stars 1.19k forks source link

undefined method `geocoded_by' for XXXXXX #1178

Open giovapanasiti opened 7 years ago

giovapanasiti commented 7 years ago

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
alexreisner commented 7 years ago

Is Tour an ActiveRecord model?

giovapanasiti commented 7 years ago

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
antillas21 commented 7 years ago

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

TrangPham commented 7 years ago

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.

alexreisner commented 6 years ago

Any updates on this? Does switching to ActiveRecord::Base solve the issue?

ATMartin commented 6 years ago

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.

alexreisner commented 6 years ago

@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.

antillas21 commented 6 years ago

@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.

alexreisner commented 5 years ago

I'm thinking the fix for #1318 may have fixed this too. Does installing the gem from current HEAD solve this for anyone?

jkogara commented 5 years ago

Installing from head doesn't fix this for me

wizardbeard commented 1 year ago

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.

This is still true in Rails 7.x+ with Ruby 3.x+. 👍

helle253 commented 4 months ago

I'm experiencing this issue on a Rails 6 project running Ruby 3.2.3

require 'geocoder'

class Region < ApplicationRecord
  extend Geocoder::Model::ActiveRecord
  validates :latitude, inclusion: { in: -90..90, message: I18n.t('latitude.invalid') },
                        presence: true
  validates :longitude, inclusion: { in: -180..180, message: I18n.t('longitude.invalid') },
                        presence: true

  reverse_geocoded_by :latitude, :longitude
end

This code is in an isolated engine, so I'm explicitly requiring geocoder as an import. @wozza35's solution worked for resolving this issue for me.