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

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

问题描述

我在 micropost 模型上创建了一个方法,该方法采用 user_id 数组.在此方法中,我使用以下查找"方法为数组中的所有用户提取所有帖子.

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 将数组转换为没有引号的昏迷分隔列表,我知道它必须很简单,但我只是想念它并且在 Google 上找不到任何可以解决此问题的内容.我发现的所有帖子都使用相同的 .join(',') 方法来协调数组.

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 或 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,则:

If args is an Array, then:

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 数组,您应该简单地调用:

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 Find 方法 IN 条件数组到参数单引号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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