Rails 3 / Ruby:ActiveRecord查找方法IN条件Array to Parameters单引号问题 [英] Rails 3 / Ruby: ActiveRecord Find method IN condition Array to Parameters single quote issue

查看:145
本文介绍了Rails 3 / Ruby:ActiveRecord查找方法IN条件Array to Parameters单引号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在微博模型上创建了一个方法,该方法采用了user_ids数组。在这种方法中,我使用以下查找方法为数组中的所有用户提取所有帖子。

I created a method on a micropost model which takes an array of user_ids. In this method I use the following 'find' method to pull all posts for all of the users within the array.

find(:all, :conditions => ["user_id IN (?)", args.join(',')])

但是,当ActiveRecord生成此查询的SQL时,它将在逗号分隔的ID中包围逗号分隔单引号。这将导致查询仅提取单引号中第一个数字的帖子,而不是所有数字。

But when ActiveRecord generates the SQL for this query it surrounds the coma delimited list of Ids in single quotes. This causes the query to only pull posts for the first number within the single quotes instead of all the numbers.

SELECT `microposts`.* FROM `microposts` WHERE (user_id IN ('3,4,5')) ORDER BY microposts.created_at DESC

查询应该看起来像这样,但是我无法弄清楚为了让Ruby将Array转换成不带引号的逗号分隔列表,我知道它必须很简单,我只是想念它,而在Google上找不到任何可以解决此问题的方法。我发现的所有帖子都使用相同的.join(’,’)方法协调Array。

The query should look like this to work correctly but I can't figure out how to get Ruby to convert the Array to a coma delimited list without the quotes, I know it has to be something simple and I am just missing it and can't find anything on Google that fixes this. All the posts I have found use the same .join(',') method to concert the Array.

SELECT `microposts`.* FROM `microposts` WHERE (user_id IN (3,4,5)) ORDER BY microposts.created_at DESC

另一个可以帮助您弄清楚我的问题的事实是ID数组,我正在通过以下方式构建方法。不知道这是否会影响事物。

One more fact which migh assist you in figuring out my issue is the Array of ids which I am passing into the method is being built in the following manner. Not sure if this would affect things.

ids = Array.new
ids.push(current_user.id)
ids = ids + current_user.friends.map(&:id)
ids = ids + current_user.reverse_friends.map(&:id)

在此先感谢可以帮助我恢复工作的人;)

Thanks in advance to the one who can help me get back to work ;)

推荐答案

更新了两次(以反映Rails 3+ ActiveRecord语法)

您可以使用以下语法:

Post.where(user_id: [1,2,3])

因此:

Post.where(user_id: args)



Rails 3 or Rails 4(原始)



出于完整性考虑,Rails 3语法为:

Rails 3 or Rails 4 (Original)

For completeness, the Rails 3 syntax would be:

Post.where("user_id IN (?)", [1,2,3]).to_a

如果args是 Array ,则:

Post.where("user_id IN (?)", args).to_a

或者,如果args是字符串数组:

Or, if args is an array of strings:

Post.where("user_id IN (?)", args.map(&:to_i)).to_a

to_a 是可选的,但将返回结果作为数组。希望对您有所帮助。

The to_a is optional, but will return the results as an array. Hope this helps.

您应该能够简单地将数组作为参数传递,如下:

You should be able to simply pass in an array as the parameter, as follows:

find(:all, :conditions => ["user_id IN (?)", [1,2,3] ] )

因此,如果args是ID数组,则应该简单地调用: / p>

Thus, if args is an array of ids, you should simply call:

find(:all, :conditions => ["user_id IN (?)", args ] )

如果类型有问题,可以这样做:

In case you have an issues with types, you can do:

find(:all, :conditions => ["user_id IN (?)", args.map { |v| v.to_i } ] )

,这将确保数组中的每个项目都是整数。

and this will ensure each item in the array is an Integer.

这篇关于Rails 3 / Ruby:ActiveRecord查找方法IN条件Array to Parameters单引号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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