Reputation: 2609
The following geocodes (gets the coordinates of the address) automagically when I create or update. This is in my Location model
def full_address
full_address = "#{address},#{city},#{state}"
end
geocoded_by :full_address
after_validation :geocode, :if => :address_changed?
But I don't want the geocoding done if I set coords_locked
to true.
I can't figure out how to do this. I tried
geocoded_by :full_address
after_validation :geocode, :if => { :coords_locked == false || :address_changed? }
What is the correct syntax or am I going about this all wrong. Thank you
PS. Just realized I probably don't need the full_address =
.
Upvotes: 0
Views: 385
Reputation: 949
I think you want the following from the Rails Guides - http://guides.rubyonrails.org/active_record_validations.html#using-a-proc-with-if-and-unless
Finally, it's possible to associate :if and :unless with a Proc object which will be called. Using a Proc object gives you the ability to write an inline condition instead of a separate method. This option is best suited for one-liners.
So perhaps something like this:
after_validation :geocode,
if: Proc.new { |location| location.coords_locked? && location.address_changed? }
Then you could maybe setup some scopes to define coords_locked?
and address_changed?
Upvotes: 1