Ransack search and translations

前端 未结 2 1752
轮回少年
轮回少年 2021-01-16 15:47

I´m trying to make ransack work on a model that has an association with a translation table.

Translations are managed by globalize gem.

The problem is when I

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-16 16:32

    Globalize gives you a class method of with_translations like:

    User.with_translations('en')
    

    And so you can set up your own scope on your model that takes advantage of this like:

    def self.with_translated_name(name_string)
      with_translations(I18n.locale).where('user_translations.name' => name_string)
    end
    

    Then you can expose this scope in your ransackable_scopes array in the model in question, like:

    private
    
    def self.ransackable_scopes   
      %i(with_translated_name) 
    end
    

    With that in place you should be able to do:

    User.ransack({ with_translated_name: "John" }) 
    

提交回复
热议问题