2011-04-12 32 views
1

在我想这一点,不知道我这样做正确:获取该COLUMN_ID所有行是ID的数组,然后才能通过ID

User.where("region_id => ?", region_ids).order("id ASC") 

region_ids = [1234,234322,234324,2343,....] 

此外,将这项工作如果region_ids是空的(不为空,但空)

我看到了一个错误:

check the manual that corresponds to your MySQL server version for the right syntax to use near '=> NULL) ORDER BY id ASC' at line 1: 

当我在调试模式下,我输出的region_ids,这是[]。

回答

2

您在字符串中混合了Ruby和SQL语法。你想要的更像是("region_id in (?)", region_ids)({ "region_id" => region_ids })

+0

大部分是正确的,但第一个例子应该是(“region_id in(?)”,region_ids)。 – 2011-04-12 03:25:56

+0

谢谢,如果/当Rails自动执行此操作时,我无法回想起我的头顶。 – Daemin 2011-04-12 03:42:16

+0

如果region_ids是[],它会在查询中显示为NULL吗?它看起来是为我... – Blankman 2011-04-13 01:03:24

0

您需要颠倒这两行的顺序,region_ids应该在User.where之前设置,因为它现在是因为当您调用where()时region_ids为NULL。

相关问题