-2

How to know if a model was already validated?

u = User.new
u.name = "Ralph"
u.valid? # => true
u.validated? # => false

I want to prevent too much queries on geocoding.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Jan
  • 12,992
  • 9
  • 53
  • 89
  • Skip in what context? What do you mean by skip? What are you geocoding? Can you post your `user.rb`? – OneChillDude Feb 18 '14 at 17:33
  • if just do `u.save` it will validate only once e return `false` if not valid. – André Barbosa Feb 18 '14 at 17:41
  • Minus three is heavy... I wanna know if this model was already "validated". It should not hit the validation process again. For example ```before_validation :geocode``` will be hitted every time when my model gets validated. But I wanna hit them only once to save my google lookup contingent. So I thought there is a "validated?" method to show me if the validation process was already done. I hope it's clearer now. – Jan Feb 18 '14 at 18:41

1 Answers1

0

If you have before_validation :geocode callback you can improve your geocode method to cache heavy code results this way:

def geocode
  @geocode_results ||= {}
  # suppose geocoding depends on `lat_lon` attribute
  @geocode_results[lat_lon] ||= begin
    # Your heavy code here
  end
end

Caching as hash value lets redo geocoding when lat_lon changes.

mikdiet
  • 9,859
  • 8
  • 59
  • 68