通过翻译的属性全球化3个订单记录并考虑回退

pri*_*kha 4 activerecord fallback ruby-on-rails globalize3

我有令人敬畏的Globalize3宝石的麻烦.现在我有两种语言:en和:ru.并且:ru回归:恩喜欢这个

#/config/initializers/globalize.rb
Globalize.fallbacks = {:ru => [:ru, :en]}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我试图通过名称翻译或翻译后备值对翻译记录的整个集合进行排序.但with_translations()似乎没有给我这样的机会!

Country.with_translations(:ru).order('country_translations.name ASC')
#this filters out those who have no :ru translations (BUT THEY SHOLD USE FALLBACKS!)
Run Code Online (Sandbox Code Playgroud)

所以要检索我可以传递一系列语言环境的所有记录:

Country.with_translations([:ru, :en]).order('country_translations.name ASC')
#but this completely ruins the sorting order (DAMN NOTHING IS SORTED)
Run Code Online (Sandbox Code Playgroud)

我想要的唯一简单的事情就是获得后备并整理所有东西!所以我们需要以某种方式获取所有记录,只按可用的名称值排序.

有什么办法吗?

pri*_*kha 18

通过挖掘Globalize3源解决了这个问题.它使用with_locales范围来获取具有适当语言环境的记录.我只需要它们:

Model.includes(:translations).
       with_locales(I18n.available_locales).
       order('model_translations.name ASC')
Run Code Online (Sandbox Code Playgroud)

希望它会帮助别人!