On Wed, Mar 9, 2011 at 12:06 AM, 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. > > are you just using .all or a complex query? Because if you're using a complex query and you're using rails3, then just remove the .all in the complex query in the controller so that it won't run the query. I think it just saves the relation to the @varieties variable and won't run the query until a method is called on the variable such .each or .collect. > 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? > > Colin > > -- > 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. > > -- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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.

