如何查找此字段或该字段位于可接受值列表中的模型

时间:2014-02-21 09:04:35

标签: ruby-on-rails postgresql activerecord rails-activerecord

如何找到起始节点或结束节点在列表中的关系。

ids = ProcessingRun.last.nodes.pluck(:id)
@rels = Relationship.where(:end_node_id => ids) + Relationship.where(:start_node_id => ids)

我在OR上尝试了很多变种

rels_in_id_array = Relationship.where("start_node_id in #{ids} OR end_node_id in#{ids}")

关系加载(0.9ms)SELECT“关系”。* FROM“关系”WHERE(start_node_id in [35752,35726,35728,35729,35731,35735,35736,35738,35742,35745,35750,35751,35723, 35725,35739,35740,35749,35724,35722,35733,35734,35737,35741,35743,35732,35746,35747,35721,35730,35727,35744,35748,35753]或者[35752,35726,35728中的end_node_id, 35729,35731,35735,35736,35738,35742,35745,35750,35751,35723,35725,35739,35740,35749,35724,35722,35733,35734,35737,35741,35743,35732,35746,35747,35721, 35730,35727,35744,35748,35753]) PG :: SyntaxError:ERROR:“[”处或附近的语法错误 第1行:......“。* FROM”关系“WHERE(start_node_id in [35752,35 ...

它没有用......

这就是为什么我以为我会将2个阵列加在一起。但事实证明它们是数组,而不是ActiveRecord关系,所以我不能用它们来做与AR关系有关的事情。

2 个答案:

答案 0 :(得分:1)

Relationship.where("start_node_id IN (?) OR end_node_id IN (?)", ids, ids)

答案 1 :(得分:0)

Relationship.where('end_node_id IN (?) OR start_node_id IN (?)', ids, ids)