Fetch main model and translations in one query with globalize2

Posted by J. Pablo Fernández on Stack Overflow See other posts from Stack Overflow or by J. Pablo Fernández
Published on 2010-06-09T14:50:25Z Indexed on 2010/06/10 8:42 UTC
Read the original article Hit count: 280

Filed under:
|
|

Is there a way to fetch the model and the translations in one query when using globalize2?

For example, having a model called Language which have two fields, code and name of which the second is translatable I do the following:

 en = Language.find_by_code("en")

and it runs this query:

SELECT SQL_NO_CACHE * FROM `languages` WHERE (`languages`.`code` = 'en') LIMIT 1

and when I do:

en.name

it runs:

SELECT SQL_NO_CACHE * FROM `language_translations` WHERE (`language_translations`.language_id = 123 AND (`language_translations`.`locale` IN ('en','root')))

and if I do it again it'll re-run the query. Is there a way to fetch all the translated data in the first query? I've tried:

en = Language.find_by_code("en", :joins => "JOIN language_translations ON language_translations.language_id = languages.id")

but it made no difference.

UPDATE: this is being discussed as an issue in globalize2: http://github.com/joshmh/globalize2/issues/#issue/33

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby