'How to order a query by translated attribute of associated model in Rails using Mobility gem

I've two associated models:

class Food < ApplicationRecord
  extend Mobility

  belongs_to :food_set, inverse_of: :foods

  translates :name, type: :string, dirty: true
end
class FoodSet < ApplicationRecord
  extend Mobility

  has_many :foods, inverse_of: :food_set

  translates :name, type: :string
end

I can perfectly do

Food.i18n.includes(:food_set).order(name: :asc)

But when I'm trying to order Foods by translated FoodSet name:

Food.i18n.includes(:food_set).order("food_sets.name": :asc)

I get PG::UndefinedColumn: ERROR: column food_sets.name does not exist (ActiveRecord::StatementInvalid)

I could write my own complex SQL query but I suspect that this should be doable directly with ActiveRecord. Any help on this will be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source