热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在rails3中将数据传递给IN查询-HowtopassdatatoINqueryinrails3

Iamworkingonrails3andsqlitedb.UsingaINquery.Currentlypassinganstringvariableofite

I am working on rails 3 and sqlite db. Using a IN query. Currently passing an string variable of items to IN query. But While executing that query it takes '' so it's not working. How to get over this situation?

我正在使用rails 3和sqlite db。使用IN查询。当前将项的字符串变量传递给IN查询。但是在执行该查询时需要''因此它不起作用。如何克服这种情况?

Here is my code

这是我的代码

items = ""
items <<"#{@invitation_user1.id}" <<"," <<"#{@invitation_user2.id}" <<"," <<"#{@user1.id}" <<"," <<"#{@user2.id}" <<"," <<"#{@user2.id}" <<"," <<"#{@profile1.id}" <<"," <<"#{@profile2.id}"
@activities = Version.where("item_id IN (?)","#{items}") 

Tried items.to_i, items.to_s but didn't work. In log i can see this.

尝试过items.to_i,items.to_s但是没有用。在日志我可以看到这一点。

SELECT "versions".* FROM "versions" WHERE (item_id IN ('19,20,4,1,1,4,1'))

But all i need is

但我所需要的只是

 SELECT "versions".* FROM "versions" WHERE (item_id IN (19,20,4,1,1,4,1))

3 个解决方案

#1


22  

You can just pass an array in. Rails is clever enough to to the right thing with it:

你可以直接传入一个数组.Rails很聪明,可以用它来做正确的事情:

items = [
  @invitation_user1.id,
  @invitation_user2.id,
  @user1.id,
  @user2.id,
  @profile1.id,
  @profile2.id
]

@activities = Version.where("item_id IN (?)", items)
# or equivalently:
@activities = Version.where(:item_id => items)

This is vastly preferred over your variant, as Rails properly handles escaping of all passed values for the used database adapter.

这比您的变体更受欢迎,因为Rails正确处理所使用的数据库适配器的所有传递值的转义。

#2


1  

Use array instead of string.

使用数组而不是字符串。

For example

例如

ids = [ @invitation_user1.id, @invitation_user2.id, @user1.id, @user2.id ]

Then easily you can find the records by

然后很容易就可以找到记录了

@versiOns= Version.find_all_by_id(ids)

This will result the query you expected.

这将导致您期望的查询。

SELECT "versions".* FROM "versions" WHERE (item_id IN (19,20,4,1))

#3


1  

What you are looking for is split:

您正在寻找的是分裂:

[1] pry(main)> a = '19,20,4,1,1,4,1'
=> "19,20,4,1,1,4,1"
[2] pry(main)> a.split(',')
=> ["19", "20", "4", "1", "1", "4", "1"]
[3] pry(main)> a.split(',').map(&:to_i)
=> [19, 20, 4, 1, 1, 4, 1]

But, as you are constructing the 'string' manually, better to use an array:

但是,当您手动构建“字符串”时,最好使用数组:

items = Array.new
items <<@invitation_user1.id <<@invitation_user2.id <<@user1.id
items <<@user2.id <<@user2.id <<@profile1.id <<@profile2.id
@activities = Version.where(item_id: items) 

Anyway, the way to get the ids is strange because... what would happen if you added more users? or profiles?

无论如何,获取ID的方式很奇怪,因为......如果添加更多用户会发生什么?或个人资料?

What I would do (seeing as little as we can see in your example)

我会做什么(在你的例子中我们看到的很少)

items = Array.new
items <

and later define those methods, as in:

然后定义这些方法,如:

def get_invitation_user_ids
  InvitationUser.select(:id).map(&:id)
end

...

推荐阅读
author-avatar
mobiledu2502936451
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有