On Tue, Mar 8, 2011 at 4:06 PM, 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?
>
> Yes, you are right. One should not access the model from the view.
Create a method in the model, say
def self.your_method_name
your_query
end
and then you can access the model in your view, using the method you just
defined in the model.
> 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.
>
>
--
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.