0

I have the following code in my User model.

  geocoded_by :city

  after_validation :geocode, if: ->(user){ user.city.present? and user.city_changed? }

Initially the user does not have to input their city so these users will not be geocoded. Is there an way to run conditional code on a call back, so if no city, geocode by ip_address, if city geocode by city?

I accept this is a basic question but I have had difficulty finding info on applying conditional code to a call back

tried

if self.city.present?
   geocoded_by :city
 else
   geocoded_by :ip_address
end

without success

GhostRider
  • 2,109
  • 7
  • 35
  • 53

1 Answers1

0

As per the docs you can pass a method to geocoded_by:

geocoded_by :geo_attr

def geo_attr
  if self.city.present?
     self.city
   else
     self.ip_address
  end
end
Subtletree
  • 3,169
  • 2
  • 18
  • 25