Rails - 嵌套关联 - 范围顺序 - 使用I18n翻译

时间:2017-02-02 13:52:39

标签: ruby-on-rails-4 join scope rails-i18n globalize

我正在使用globalize和i18n。 我有以下型号:

class Result < ActiveRecord::Base
    belongs_to :test
    ...
    scope :sort_with_teaser, lambda { |direction|
        joins(test: :test_translations).
            where(test_translations: {locale: 'fr'}).
            order("test_translations.teaser #{ direction }")        
        }
end

class Test < ActiveRecord::Base
    translates :teaser
    ...
end

因此,对于Globalize和I18n,我还有一个test_translations表。

我想在Result模型中实现范围sort_with_teaser,以使用result.test.teaser对结果进行排序。但我这段代码不起作用。

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

好的,我刚修好了:

joins(test: :translations).where(test_translations: {locale: 'fr'}).order("test_translations.teaser #{ direction }")