Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9288407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:54:32+00:00 2026-06-18T19:54:32+00:00

I am using geocoder and have a model called Location. This models contains longitude,

  • 0

I am using geocoder and have a model called Location. This models contains longitude, latitude, and address. What I would like is to give the option to the user to fill in either longitude and latitude or the address. If one of them is fill up then fill up the remaining information. So here what I did

  before_validation :check
  def check
    if self.address.blank?
        reverse()
    else
        geo()
    end
  end

  def reverse
    reverse_geocoded_by :latitude, :longitude,
    :address => :address
    after_validation :reverse_geocode
  end

  def geo
    geocoded_by :address
    after_validation :geocode
  end

But i am getting the following undefined method reverse_geocoded_by I am not sure what to do!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:54:34+00:00Added an answer on June 18, 2026 at 7:54 pm

    You can’t call class methods inside an instance method. Remember that geocoded_by is probably defined for your model class. You cannot arbitrarily change the validations of one instance without affecting all instances of the model, so this is generally viewed as a bad idea.

    You might be better off having a “Location” model that can be geocoded one of two different ways, and creating a sub-class for each encoding type. That is, the rules can be established for each of these independently. Storing these is easy using Single Table Inheritance (STI).

    So you could have:

    class Location < ActiveRecord::Base
    end
    
    class ForwardLocation < Location
      geocoded_by :address
      after_validation :geocode
    end
    
    class ReverseLocation < Location
      reverse_geocoded_by :latitude, :longitude,
        :address => :address
      after_validation :reverse_geocode
    end
    

    Then you’d add the proper location accordingly:

    self.location = self.address? ? ReverseLocation.create(...) : ForwardLocation.create(...)
    

    You could also write methods that handle populating the fields more directly than having to use subclasses like this, but it depends entirely on the kind of data you’re receiving and processing.

    As a rule, a class should have only one set of before/after callbacks. It can have some of these callbacks triggered conditionally if the callback definition supports an :if style declaration. Sometimes you need to write your own before_validation routines to handle your more exotic requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the rails geocoder gem and have the following in my location model
I have a Ruby on Rails model Camping with #latitude and #longitude attributes. This
I'm using the android.location.Geocoder for the first time. The Idea is: I have a
I am using ASP.NET MVC 4 and I have a DIV like this: <div
I have difficulties with the search location radius function using with geocoder and a
I have some worker thread that gets an address using the geocoder, and when
I get latitude and longitude values as geocoder results by using codes I got
I have developed an application to find the user's location using GPS. In this
I have done this function, using geocoder module, to get the city from a
I have this js code where I am using the Mapstraction library to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.