My model looks like this:
class Item < ActiveRecord::Base
has_many :locations
validate :validate_item_location
def item_location
locations.address+','+locations.city+','+locations.country
end
def item_location=(str)
geo = Geokit::Geocoders::MultiGeocoder.geocode(str)
if geo.success
locations.build( :lat => geo.lat, :lng => geo.lng)
end
end
def validate_item_location
geo = Geokit::Geocoders::MultiGeocoder.geocode( item_location )
errors.add_to_base("Location is invalid") unless geo.success
end
end
My questions 1. How to correctly write a getter method item_location defined? 2. How can I validate item_location field. I created validate_item_location method but don't know how to get item_location variable inside when I POST data through my form. 3. Is my setter method ok?
THX!