【问题标题】:Mobility gem + Rails : how to perform a join with LIKE query on a translated modelMobility gem + Rails:如何在翻译模型上使用 LIKE 查询执行连接
【发布时间】:2018-07-02 09:19:22
【问题描述】:

在我的应用程序上实现 Mobility 之前,我已经让这个查询工作了:

Product.joins(:category).where('categories.name ILIKE ANY ( array[?] )', categories)

类别为= params[:categories].map {|category| "%#{category}%" }

在实现了 Mobility gem 之后,查询输出明显是:[]

因此我尝试添加 .i18n,如 gem doc 中所述:Product.i18n.joins(:category).where('categories.name ILIKE ANY ( array[?] )', categories)

--> 输出 [],因为它不加入已翻译的表:SELECT "products".* FROM "products" INNER JOIN "categories" ON "categories"."id" = "products"."category_id" WHERE (categories.name ILIKE ANY ( array['%Categorie test%'] ))

然后我尝试加入类别翻译表,但没有成功。我试图用following question 激发我的查询。但是所有这些查询都失败了:

Product.i18n.joins(:category).joins(:translations).where('categories.name ILIKE ANY ( array[?] )', categories)
Product.i18n.joins(:category).join_translations.where('categories.name ILIKE ANY ( array[?] )', categories)
Product.i18n.joins(:mobility_string_translations).where('categories.name ILIKE ANY ( array[?] )', categories)
Product.i18n.joins(:category_name_fr_string_translations).where('categories.name ILIKE ANY ( array[?] )', categories)

返回:undefined method join_translationsCan't join 'Product' to association association_name '

如何使用 ILIKE 加入类别和查询以获得翻译输出?

【问题讨论】:

标签: ruby-on-rails activerecord mobility


【解决方案1】:

感谢 Chris 的帮助,以下是可能感兴趣的人的 Arel 查询:

Product.joins(:category).merge(Category.i18n {name.matches_any(categories)})

【讨论】:

  • 哇,这真是太棒了!我不熟悉“匹配任何”,所以不知道 Arel 方法会是什么,但您似乎已经找到了。这样做的好处是它可以与任何后端一起使用。我计划创建一个关于查询的 wiki 页面,因为像这样的东西(在 0.7 中添加)打开了许多有趣的可能性的大门。我将在此处添加此示例:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 2017-06-04
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多