从阵列中创建一个轨道,其中查询 [英] Creating a where query in rails from an array

查看:141
本文介绍了从阵列中创建一个轨道,其中查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个数组,其中数组中的每个成员是相与一个喜欢的操作使那里查询。例如:

I need to make a where query from an array where each member of the array is a 'like' operation that is ANDed. Example:

SELECT ... WHERE property like '%something%' AND property like '%somethingelse%' AND ...

这是很容易使用的ActiveRecord 其中,函数来完成,但我不能确定如何先清理它。我显然不能只创建一个字符串和东西在其中,的功能,但似乎并没有成为一个可能的方式使用

It's easy enough to do using the ActiveRecord where function but I'm unsure how to sanitize it first. I obviously can't just create a string and stuff it in the where function, but there doesn't seem to be a way possible using the ?.

感谢

推荐答案

要建立你LIKE模式最简单的方法是字符串插值:

The easiest way to build your LIKE patterns is string interpolation:

where('property like ?', "%#{str}%")

如果你有一个数组中所有的字符串,那么你可以使用ActiveRecord的查询链接和的 来构建最终的查询:

and if you have all your strings in an array then you can use ActiveRecord's query chaining and inject to build your final query:

a = %w[your strings go here]
q = a.inject(YourModel) { |q, str| q.where('property like ?', "%#{str}%") }

然后就可以 q.all q.limit(11)或任何你需要做的就是你的最终结果。

Then you can q.all or q.limit(11) or whatever you need to do to get your final result.

下面是关于如何工作的快速教程;您应该查看活动记录查询界面指南和的 可枚举文档为好。

Here's a quick tutorial on how this works; you should review the Active Record Query Interface Guide and the Enumerable documentation as well.

如果你有两件事情( A B )来匹配,你可以这样做:

If you had two things (a and b) to match, you could do this:

q = Model.where('p like ?', "%#{a}%").where('p like ?', "%#{b}%")

其中,方法返回一个支持所有常用的查询方法的对象,所以你可以调用链为 M.where(...)。其中,(...)... 根据需要;其他查询方式(如限制,...)返回相同的类的对象,所以你可以连锁的还有:

The where method returns an object that supports all the usual query methods so you can chain calls as M.where(...).where(...)... as needed; the other query methods (such as order, limit, ...) return the same sort of object so you can chain those as well:

M.where(...).limit(11).where(...).order(...)

您有事情要LIKE对数组,你想申请其中,的模型类,然后应用其中,什么返回,然后再次直到你已经用完了您的阵列。事情看起来像一个反馈回路往往要求 注入 (又名减少从地图,减少成名):

You have an array of things to LIKE against and you want to apply where to the model class, then apply where to what that returns, then again until you've used up your array. Thing that look like a feedback loop tend to call for inject (AKA reduce from "map-reduce" fame):

注入(初始){|备忘录,OBJ |块}→OBJ

通过应用二进制运算,以块或符号名称的方法或者操作者指定的结合的枚举的所有元素。

Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.

如果您指定块,然后在枚举的块传递一个累加值每个元素(备忘录的)和元素[...]结果就变成为的备忘录的新值的。在迭代结束,中的备忘录的最终价值的是该方法的返回值。

If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element [...] the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.

所以获取块的输出(这是返回值其中,在我们的例子),并供稿作为输入到该块的下一次执行。如果你有一个数组,你就可以了:

So inject takes the block's output (which is the return value of where in our case) and feeds that as an input to the next execution of the block. If you have an array and you inject on it:

a = [1, 2, 3]
r = a.inject(init) { |memo, n| memo.m(n) }

那么这就是与此相同:

then that's the same as this:

r = init.m(1).m(2).m(3)

或者,在伪code:

Or, in pseudocode:

r = init
for n in a
    r = r.m(n)

这篇关于从阵列中创建一个轨道,其中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆