title: caching table values (avoiding unnecessary find queries) - is this a good approach?
body: <p>In my app i often need to find brand id by brand name in many places, so what if...</p>
  
  <pre><code>class Brand &lt; ActiveRecord::Base
  
  ...
  
    after_save :refresh_names_hash
  
    #this is a rare action because brands list is static enough
    def refresh_names_hash
      Brand.names_hash(true)
    end
  
    def self.names_hash(refresh=false)
      @names_hash = nil if refresh
      @names_hash ||= Hash[all.map{|b| [b.name, b.id]}]
    end
  
  ...
  
  end
  </code></pre>
  
  <p>Hope everything is clear enough from this code</p>
  
  <p>And here is a question: is this a good approach to use <code>Brand.names_hash[params[:name]]</code> to achieve brand id every time i need it instead of query it from db in production environment (classes cache  enabled)?</p>