Created
May 25, 2012 08:46
-
-
Save Fernan2/2786697 to your computer and use it in GitHub Desktop.
Hash de provincias #hack #performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Si tengo que acceder a la provincia en un bucle de 10.000 iteraciones, es poco eficiente hacer | |
Provincia.nombre(rest.provincia_id) | |
# En vez de eso, me creo un hash de provincias: {1=>"Álava/Araba", 2=>"Albacete", 3=>"Alicante",...} | |
provincias = {} | |
Provincia.select('id, nombre').each{ |p| provincias.merge!({ p.id => p.nombre }) } | |
# Y ya puedo acceder directamente sin llamar a nadie más: | |
provincias[rest.provincia_id] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment