Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globalize3 order records by translated attributes and taking fallbacks into consideration

Im having troubles with awesome Globalize3 gem. For now I`m having two languages :en and :ru. And :ru falls back to :en like this

#/config/initializers/globalize.rb
Globalize.fallbacks = {:ru => [:ru, :en]}

In my controller I am trying to sort the whole collection of translated records either by name translations or by translations fallback values. But with_translations() does not seem to give me such opportunity!

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

so to retrieve all records i can pass an array of locales:

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

and the only simple thing i want is to get fallbacks and sorting all togather! So we need somehow get all records only sorted by available name value.

Is there any way?

like image 464
prikha Avatar asked Nov 29 '22 01:11

prikha


1 Answers

Solved this by digging into Globalize3 source. It uses with_locales scope to get records that have proper locales present. I just needed them all:

Model.includes(:translations).
       with_locales(I18n.available_locales).
       order('model_translations.name ASC')

Hope it will help someone!

like image 192
prikha Avatar answered Apr 20 '23 00:04

prikha