Rails数组条件查询

Rya*_*ing 0 ruby-on-rails

我已经写了下面的方法来结合ReferencesSections模式,它的孩子:

def combined_references
    ids = []
    ids << self.id
    self.children.each do |child|
      ids << child.id
    end
    Reference.where("section_id = ?", ids)
  end
Run Code Online (Sandbox Code Playgroud)

但是section.combined_references返回以下错误:

Mysql2::Error: Operand should contain 1 column(s): SELECT `references`.* FROM `references`  WHERE (section_id = 3,4)
Run Code Online (Sandbox Code Playgroud)

它似乎收集了id的正确值,我是否错误地构造了查询?

Zip*_*pie 5

将最后一行转换为:

Reference.where(section_id: ids)
Run Code Online (Sandbox Code Playgroud)

它应该产生:

SELECT `references`.* FROM `references`  WHERE section_id IN (3,4)
Run Code Online (Sandbox Code Playgroud)

您可以将代码缩短一行:

 ids = []
 ids << self.id
Run Code Online (Sandbox Code Playgroud)

 ids = [self.id]
Run Code Online (Sandbox Code Playgroud)