views:

665

answers:

2

I’m using latest globalize2 and rails 2.2. I wonder if the following is bug or feature: there seems to be a separate db query for each item in a dataset to get the translation. That doesn’t sound right since it may easily result in hundreds of queries.

Illustration. Simple controller:

def index
    @menu_sections = MenuSection.find(:all)
end

Then @menu_sections is looped thru in a view, where localized attribute (name) is called:

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

Looks like every menu_section.name results in db query:

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]

What do you think? Perhaps there is a better way for translating db data in rails?

A: 

I don'k know if you reslove this issue. But I am ran into the same problem. I an translating just one column of a table (say title). If I have 100 rows in a table and if I do a query like this for a given locale:

rows = Category.find(:all, .....)

It will result in 101 queries on the database !! I am not sure if this is expected or intended by the designer of Globalize.

OR I am missing something (Like an optional configuration parameter on the query).

However I did find an alternative solution by Saimon Moore who has implemented Alternative implementation of Globalize Model Translations. You can read about it at:

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

However I can't find any references if this works with Globalize2.

I am at a point of throwing out Globalize2 plugin and roll my own solution using Ruby I18N library.

Prakash Teli
+2  A: 

You can accomplish what you're trying to do by saying something like:

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

That will eager load the translations, so, there will only be 2 queries.