我已经写了下面的方法来结合References的Sections模式,它的孩子:
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的正确值,我是否错误地构造了查询?
将最后一行转换为:
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)
| 归档时间: |
|
| 查看次数: |
2660 次 |
| 最近记录: |