문제

나는 최신 Globalize2와 Rails 2.2를 사용하고 있습니다. 다음이 버그인지 기능인지 궁금합니다. 데이터 세트의 각 항목에 대해 번역을 가져 오기 위해 별도의 DB 쿼리가있는 것 같습니다. 수백 개의 쿼리가 쉽게 발생할 수 있기 때문에 소리가 나지 않습니다.

삽화. 간단한 컨트롤러 :

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]

어떻게 생각해? 아마도 레일에서 DB 데이터를 번역하는 더 좋은 방법이 있습니까?

도움이 되었습니까?

해결책

당신은 다음과 같은 말을함으로써 당신이하려는 일을 성취 할 수 있습니다.

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

그것은 번역을 열망 할 것이므로 2 쿼리 만있을 것입니다.

다른 팁

이 문제를 재개하는지 모르겠습니다. 그러나 나는 같은 문제에 부딪쳤다. 나는 테이블의 한 열만 번역을 번역했다 (제목). 테이블에 100 행이 있고 주어진 로케일에 대해 이와 같은 쿼리를하는 경우 :

행 = 범주 .find (: All, .....)

데이터베이스에서 101 개의 쿼리가 발생합니다 !! 글로벌 디자이너가 이것이 예상되는지 또는 의도하는지 확실하지 않습니다.

또는 쿼리의 선택적 구성 매개 변수와 같은 무언가가 누락되었습니다.

그러나 나는 세계화 모델 번역의 대체 구현을 구현 한 Saimon Moore의 대안 솔루션을 찾았습니다. 다음에 대해 읽을 수 있습니다.

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

그러나 이것이 Globalize2와 함께 작동하는 경우 참고 문헌을 찾을 수 없습니다.

나는 globalize2 플러그인을 버리고 Ruby i18N 라이브러리를 사용하여 내 솔루션을 굴립니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top