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
这种做法是否合理?
请不要建议