Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting model instances with Globalize3

The problem seems trivial, but I can't find any reasonable solution. I have list of countries with translations stored in Globalize3 translation tables. How can I fetch the list of countries sorted by name?

Country name isn't stored directly in the model, but in separate table. Is there any reasonable way to sort the result other than manual sql query, or manualy sorting result table after AR query is complete?

like image 276
mdrozdziel Avatar asked Apr 09 '26 12:04

mdrozdziel


2 Answers

Country.with_translations(I18n.locale).order('name') for current locale.

Edit:

You can also use fallbacks:

Country.with_translations(I18n.fallbacks[I18n.locale]).order('name')

like image 179
Mirko Avatar answered Apr 12 '26 01:04

Mirko


Country.with_translations.order('name')
like image 35
rawonstack Avatar answered Apr 12 '26 00:04

rawonstack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!