Ransack search and translations

前端 未结 2 1777
轮回少年
轮回少年 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

    While @rlarcombe his answer works in this specific situation, you loose all predicates that ransack provides (eq, cont, etc.) and are basically writing the search query yourself.

    Globalize adds the translation table as an association and ransack provides the ability to search on associated tables by prefixing the searchable attribute with the associated table name.

    For your situation this would work and would still allow you to use all of ransack's predicates.

    User.with_translations(I18n.locale).ransack(translations_name_eq: 'John')
    

    You can use other predicates like cont (contains) for ILIKE matching by just replacing the predicate suffix:

    User.with_translations(I18n.locale).ransack(translations_name_cont: 'John')
    

提交回复
热议问题