2009-03-02 62 views
0

我正在使用最新的globalize2和rails 2.2。我不知道以下是錯誤還是功能:對於數據集中的每個項目似乎都有單獨的數據庫查詢來獲取翻譯。這聽起來不對,因爲它可能會導致數百個查詢。使用Globalize2(rails)翻譯模型

插圖。簡單的控制器:

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 %> 

看起來像DB查詢每menu_section.name結果:

 
Processing StoreController#index (for 10.0.2.2 at 2009-03-02 16:05:53) [GET] 
    Session ID: 468f54928cbdc0b19c03cfbd01d09fa9 
    Parameters: {"action"=>"index", "controller"=>"store"} 
    MenuSection Load (0.0ms) SELECT * FROM `menu_sections` 
Rendering template within layouts/store 
Rendering store/index 
Rendered application/_js_includes (0.0ms) 
    MenuSection Columns (0.0ms) SHOW FIELDS FROM `menu_sections` 
    MenuSectionTranslation Load (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'))) 
Completed in 340ms (View: 320, DB: 0) | 200 OK [http://www.dev.babooka.com/store] 

您怎麼看?也許有更好的方式來轉換軌道中的數據庫數據?

回答

2

你可以完成你想要說什麼樣的事:

def index 
    @menu_sections = MenuSection.find(:all,:include=>:globalize_translations) 
end 

這將渴望負荷翻譯,所以,只會有2個查詢。

0

我不知道你是否喜歡這個問題。但我遇到了同樣的問題。我只翻譯一列表中的一列(比如標題)。如果我在一個表中有100行,如果我做一個查詢這樣對於一個給定的語言環境:

行= Category.find(:全部.....)

這將導致101個查詢在數據庫上!我不確定這是Globalize的設計者預期的還是意圖的。

或者我缺少一些東西(如查詢中的可選配置參數)。

但是我確實找到了Saimon Moore的替代解決方案,他已經實施了Globalize Model Translations的替代實現。你可以在閱讀它:

http://saimonmoore.net/2006/12/1/alternative-implementation-of-globalize-model-translations

但是我找不到任何引用,如果這個工程與Globalize2。

我正在拋出Globalize2插件,並使用Ruby I18N庫推出我自己的解決方案。