我试图通过收集id的数组来将数据渲染到样条图表上:
控制器
def student_feedback
difficulty_ids = current_user.school_user.homework_students.pluck(:difficulty)
@homeworks = HomeworkStudent.where("homework_id = (?)", difficulty_ids).first
end
在我的图表中:
...
}
},
series: [{
name: 'Difficulty',
data: [<%= @homeworks.to_json %>]
}]
});
但我一直收到这个错误:
Mysql2::Error: Operand should contain 1 column(s): SELECT `homework_students`.* FROM `homework_students` WHERE (homework_id = (4,2,1,2,3)) ORDER BY `homework_students`.`id` ASC LIMIT 1
收集的数据是正确的。这只是一个语法错误吗?我玩了一下。从&#34; homework_id =?&#34;中删除括号给出相同的结果......如果需要可以提供更多代码。
由于
谢谢@ the12
试过这个
def student_feedback
difficulty_ids = current_user.school_user.homework_students.pluck(:difficulty)
@homeworks = HomeworkStudent.where(["homework_id = ?", difficulty_ids]).first
end
但这会返回错误`
Mysql2::Error: Operand should contain 1 column(s): SELECT `homework_students`.* FROM `homework_students` WHERE (homework_id = 4,2,1,2,3) ORDER BY `homework_students`.`id` ASC LIMIT 1`
如果它说homework_id = 4,2,1 ......这些数字实际上来自一个名为&#34;难度&#34; - 也许在控制器上面的陈述中有些东西是关闭的?
答案 0 :(得分:1)
只是读取SQL错误.. difficulty_ids
是一个数组,由它产生的SQL语句证明:
WHERE (homework_id = 4,2,1,2,3)
从homework_id = difficulty_ids
开始,它是一个数组。您必须确保homework_id
仅等于where子句中的单个数字。