문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top