【发布时间】:2009-03-02 21:31:04
【问题描述】:
我正在使用最新的 globalize2 和 rails 2.2。我想知道以下是否是错误或功能:数据集中的每个项目似乎都有一个单独的数据库查询来获取翻译。这听起来不对,因为它可能很容易导致数百个查询。
插图。简单的控制器:
def index
@menu_sections = MenuSection.find(:all)
end
然后@menu_sections 在视图中循环,其中调用本地化属性(名称):
<% @menu_sections.each do |menu_section| %>
<p><%= link_to menu_section.name, :controller => 'store', :action => 'list_menu_items_for_section', :section_id => menu_section.id %></p>
<% end %>
看起来每个 menu_section.name 都会导致 db 查询:
处理 StoreController#index(2009-03-02 16:05:53 的 10.0.2.2)[GET] 会话 ID:468f54928cbdc0b19c03cfbd01d09fa9 参数:{"action"=>"index", "controller"=>"store"} MenuSection 加载 (0.0ms) SELECT * FROM `menu_sections` 在布局/商店中渲染模板 渲染存储/索引 渲染的应用程序/_js_includes (0.0ms) MenuSection Columns (0.0ms) SHOW FIELDS FROM `menu_sections` MenuSectionTranslation 加载 (0.0ms) SELECT * FROM `menu_section_translations` WHERE (`menu_section_translations`.menu_section_id = 1 AND (`menu_section_translations`.`locale` IN ('en','root'))) MenuSectionTranslation Columns (0.0ms) SHOW FIELDS FROM `menu_section_translations` MenuSectionTranslation Load (0.0ms) SELECT * FROM `menu_section_translations` WHERE (`menu_section_translations`.menu_section_id = 2 AND (`menu_section_translations`.`locale` IN ('en','root'))) MenuSectionTranslation Load (0.0ms) SELECT * FROM `menu_section_translations` WHERE (`menu_section_translations`.menu_section_id = 3 AND (`menu_section_translations`.`locale` IN ('en','root'))) 在 340 毫秒内完成(查看:320,DB:0)| 200 OK [http://www.dev.babooka.com/store]你怎么看?也许有更好的方法来翻译 Rails 中的 db 数据?
【问题讨论】:
标签: ruby-on-rails localization