Rails 3 - using cache for form view helpers grouped_collection_select -


i have recurrent view in form in data select tags doesn't change expensive, want implement cache, works selects don't know how make work grouped_collection_select.

this code want cache (sorry spanish inflections):

models code:

def self.cached_campuses   rails.cache.fetch([self, "campuses"])     campus.active.order(:name)   end end  def self.cached_programas   rails.cache.fetch([self, "programas"])     programa.considered.active.order(:name).map{ |programa| [programa.name, programa.id, {'data-campus'=>programa.campus.id, 'data-nivel'=>programa.nivel.clave}] }   end end #there more of same style 

controller:

@campus = campus.cached_campuses @programas = programa.cached_programas # others 

view:

= simple_form_for @user |f|   # reads cache   = f.input :campus_id, collection: @campus, ...    # 1 doesn't read cache, makes queries table nivel cause grouped_collection   = f.grouped_collection_select :nivel_id, @campus, :niveles, ...    # reads cache   = f.select :programa_id, options_for_select( @programas )   # other selects 

i need select grouped_collection because in view filtered via javascript depending on option user selected in top select.

how can cache inside model nivel data grouped_collection_select ?

found it,

instead of grouped_collection_select, in helper did:

def cached_niveles   unless rails.cache.exist?( "select_niveles" )     rails.cache.write( "select_niveles", option_groups_from_collection_for_select(campus.cached_campuses, :niveles, :id, :clave, :nombre) )   end    rails.cache.read( "select_niveles") end 

and form:

= select_tag 'usuario[nivel_id]', cached_niveles, prompt: "nivel de interÉs" 

the trick use option_groups_from_collection_for_select


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -