Arel:动态生成价值数组的条件
我希望在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀 - 阿雷尔(Arel)非常容易。
我仍然想了解更多有关链接Arel条件的信息。
Whoops - it's super easy with Arel.
I'd still like to know more about chaining Arel conditions.