Ruby on rails 搜索和翻译

Ruby on rails 搜索和翻译,ruby-on-rails,search,translation,ransack,globalize,Ruby On Rails,Search,Translation,Ransack,Globalize,我试图让ransack在一个与翻译表有关联的模型上工作 翻译由globalize gem管理 问题是,当我尝试在模型表的:name列中使用ransack进行搜索时,它什么也没有显示,因为:name信息存储在另一个表中 有什么线索我可以在关联翻译表中进行搜索吗?我知道有可能会使用Ransacker,但我不知道应该将哪些代码放入其中。Globalize为您提供了类似的类方法: 因此,您可以在模型上设置自己的范围,以利用以下优势: def self.with_translated_name(name_

我试图让ransack在一个与翻译表有关联的模型上工作

翻译由globalize gem管理

问题是,当我尝试在模型表的:name列中使用ransack进行搜索时,它什么也没有显示,因为:name信息存储在另一个表中


有什么线索我可以在关联翻译表中进行搜索吗?我知道有可能会使用Ransacker,但我不知道应该将哪些代码放入其中。

Globalize为您提供了类似的类方法:

因此,您可以在模型上设置自己的范围,以利用以下优势:

def self.with_translated_name(name_string)
  with_translations(I18n.locale).where('user_translations.name' => name_string)
end
然后,您可以在所讨论的模型中公开它,如:

private

def self.ransackable_scopes   
  %i(with_translated_name) 
end
有了这一点,您应该能够做到:

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

Globalize为您提供了类似的类方法:

因此,您可以在模型上设置自己的范围,以利用以下优势:

def self.with_translated_name(name_string)
  with_translations(I18n.locale).where('user_translations.name' => name_string)
end
然后,您可以在所讨论的模型中公开它,如:

private

def self.ransackable_scopes   
  %i(with_translated_name) 
end
有了这一点,您应该能够做到:

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

虽然@rlarcombe的答案在这种特定情况下有效,但您失去了ransack提供的所有谓词(eq、cont等),基本上是自己编写搜索查询

Globalize将转换表添加为关联,而ransack通过在可搜索属性前面加上关联表名,提供了在关联表上搜索的能力

对于您的情况,这将起作用,并且仍然允许您使用ransack的所有谓词

User.with_translations(I18n.locale).ransack(translations_name_eq: 'John')
您可以使用其他谓词,如
cont
(contains)来进行
ILIKE
匹配,只需替换谓词后缀:

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

虽然@rlarcombe的答案在这种特定情况下有效,但您失去了ransack提供的所有谓词(eq、cont等),基本上是自己编写搜索查询

Globalize将转换表添加为关联,而ransack通过在可搜索属性前面加上关联表名,提供了在关联表上搜索的能力

对于您的情况,这将起作用,并且仍然允许您使用ransack的所有谓词

User.with_translations(I18n.locale).ransack(translations_name_eq: 'John')
您可以使用其他谓词,如
cont
(contains)来进行
ILIKE
匹配,只需替换谓词后缀:

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

这应该是正确的答案。通过这种方式,您可以使用与
globalize
相同的关联。此外,如果使用_translations()删除
,这也适用于搜索所有翻译。这应该是正确的答案。通过这种方式,您可以使用与
globalize
相同的关联。此外,如果使用_translations()
删除
,这也适用于搜索所有翻译。