On 8 Mar 2011, at 16:06, Colin Law <[email protected]> wrote: > I am using fragment caching to render a select box where the contents > of the select box comes from a lengthy db query. A simplified example > showing the problem is > <% cache 'select_box' do %> > <%= f.collection_select :variety_id, @varieties, :id, :name %> > <% end %> > where @varieties is setup in the controller. Unfortunately this does > not achieve the desired result as the query is run even when the > select is picked up from the cache. > > To get the full benefit I have to remove @varieties = Variety.all from > the controller and use > <% cache 'select_box' do %> > <%= f.collection_select :variety_id, Variety.all, :id, :name %> > <% end %> > but this breaks the rule that one should not access the model from the > view. Is there a good solution to this problem?
In the controller, you can check for the presence of the cached fragment using the read_fragment method. Then, only run the database query if the fragment isn't cached yet. Chris -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

