Arel:动态生成价值数组的条件

发布于 2025-01-27 15:54:20 字数 926 浏览 4 评论 0原文

我希望在Rails应用程序中为查询生成“或”条件,从我想尝试匹配对单个列中的值的单词。我正在尝试在a with a 好答案

但是这个问题中的OP遗漏了解释方法value.to_condition他将其值转化为条件。基本上,我不明白如何将每个单词字符串变成可链的Arel条件。

在链接SQL查询字符串时,这种事情很容易理解,但是在Arel中,我不知道如何在不执行这些条件的情况下链接条件 - 条件是笔直的Ruby。

在下面,模式是我的单词数组。每个条件都应table.arel_table [:text] .matches(staters),我希望每一个条件都与连接在一起

conditions = patterns.inject { |conds, cond| cond.or(conds) }

。但是,我不明白链接是如何工作的。

conditions = patterns.inject do |conds, cond|
  Table.arel_table[:text].matches(pattern).or(conds) 
end

I'm looking to generate "or" conditions for a query in a Rails app, from an array of words i want to try to match against values in a single column, in Arel. I'm trying to follow advice in a very similar SO question with a good answer.

But the OP in that question left out explaining the method value.to_condition where he turns his values into conditions. Basically i don't understand how to turn each word string into a chainable Arel condition.

This kind of thing is easy to understand for me when chaining SQL query strings, but in Arel, I don't know how to chain the conditions without executing them — the conditions are straight Ruby.

In the below, patterns is my array of words. Each condition should be phrased Table.arel_table[:text].matches(pattern) and I'd like each of these conditions joined with an .or

conditions = patterns.inject { |conds, cond| cond.or(conds) }

When I try to do this though, I don't understand if or how the chaining is working.

conditions = patterns.inject do |conds, cond|
  Table.arel_table[:text].matches(pattern).or(conds) 
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平生欢 2025-02-03 15:54:20

哎呀 - 阿雷尔(Arel)非常容易。

Table.where(Table.arel_table[:text].matches_any(patterns))

我仍然想了解更多有关链接Arel条件的信息。

Whoops - it's super easy with Arel.

Table.where(Table.arel_table[:text].matches_any(patterns))

I'd still like to know more about chaining Arel conditions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文