Ransack 搜索和翻译 [英] Ransack search and translations
问题描述
我正在尝试对与翻译表有关联的模型进行搜查.
I´m trying to make ransack work on a model that has an association with a translation table.
翻译由 globalize gem 管理.
Translations are managed by globalize gem.
问题是当我尝试在 :name 列的模型表中使用 ransack 进行搜索时,它什么也没显示,因为 :name 信息存储在另一个表中.
The problem is when I try to search with ransack in model table in :name column, it shows nothing because the :name information is stored in another table.
任何线索如何在关联翻译表中进行搜查搜索?我知道有可能使用 ransackers,但我不知道要放入哪些代码.
Any clues how can I make ransack search in the association translation table? I know there is the possibility of using ransackers, but I have no idea which code to put in them.
推荐答案
Globalize 给你一个 with_translations 喜欢:
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
然后你可以在你的ransackable_scopes数组中公开这个范围 在相关模型中,例如:
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" })
这篇关于Ransack 搜索和翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!