English 中文(简体)
铁路非投标数据清单
原标题:Best practice for caching expensive lists of non-rendered data in Rails

This is a general question, but to get the point across, let s discuss a specific example. Suppose you have an application that frequently uses forms that include a list of all the countries in the world. The countries are stored in the countries table in your DB, but this table is updated very very rarely, and only through your seeds.rb file.

为了能够在每一次请求中花一点时间,我通常处理下列事项:

module ApplicationHelper

    def self.get_countries
        countries = Country.order("name asc").all.collect{|country| [country.name, country.name]}
        countries.unshift(["",""])
        countries
    end

    # In production mode (cache_classes = true) this is executed only once, and 
    # will remain cached until you restart your application server
    @@COUNTRIES =  ApplicationHelper.get_countries

    # This would be called from various views
    def countries_for_select(country)
        selected = country.name unless country.nil?
        options_for_select(@@COUNTRIES, selected)
    end
end

这种做法是否合理?

请不要建议而不是在数据库中储存国家名单。 我知道还有其他管理国家名单的方法。 我并不真正关心各国——它只是最容易理解的例子,我认为可以说明这个一般性问题。

问题回答

我不肯定你通过“非投放数据”的含义。 采用你的方法,形式观点仍需要贯穿所有国家,为每个国家的选择提供选择。

<select id="user_country" name="user[country]">
  <option value="US">United States</option>
  <option value="CA">Canada</option>
  <option value="ET">Etcetera</option>

但是,如果你在一次部署后第一次把选编成碎片,那么,将首先提出选择,然后将选编成一个单独的文件,即文件/文件/文件。 之后每当表格装上时,铁路将使用该卡路子,如其部分装入表格。

<% form_for @user do |form| %>
  <% cache( user_countries_selector ) do %>
    <% form.select :country, Country.order("name asc").all.collect{|country [country.name, country.name]}
  <% end %>
  <% etc, etc, etc %>
<% end %>

尽管在实践中,我可能把<代码>移至此处。 国家.order... 编码为助手法。 将国家名单编为@COUNTRIES是完全没有必要的。

如果你需要以其他形式选择一个国家,那么你就应该通过另一个关键来<条码>cache(),因为所选标的国名和国名将具体化。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签