MySQL,Highcharts:“操作数应该包含1列:”错误(后续) [英] MySQL, Highcharts: “operand should contain 1 column(s):” error (follow up)
问题描述
控制器
我试图通过收集ID数组来呈现数据到样条图上。 pre> def student_feedback
difficulty_ids = current_user.school_user.homework_students.pluck(:difficulty)
@homeworks = HomeworkStudent.where(homework_id =(?),difficulty_ids ).first
end
我的目标表称为homework_students:
create_tablehomework_students,force::cascade do | t |
t.integerschool_user_id,限制:4,null:false
t.integerhomework_id,限制:4,null:false
t.datetimecompleted_on
t.datetimecreated_at,null:false
t.datetimeupdated_at,null:false
t.booleancompleted,limit:1
t.integerdifficulty,limit :4
t.integerinterest,limit:4
end
每个学生都有许多不同的家庭作业,他们可以在每个难度栏中分配一个难度值。我正在为每个学生绘制这些数据。
因此,在上面的控制器动作中,我正在为当前用户在数组中关联的每项作业摘取难度数字(1 - 5)。
在我的图表中:
...
}
},
系列:[{
名称:'难度',
数据:[<%= @ homeworks.to_json%>]
}]
});
我尝试将操作更改为此:
def student_feedback
difficulty_ids = current_user.school_user.homework_students.pluck(:difficulty)
@homeworks = HomeworkStudent.where([homework_id =?,difficulty_ids]) .first
end
我一直收到这个错误:
Mysql2 :: Error:操作数应该包含1列: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 ..(这是不正确的,这些是难度值)。我怀疑这只是一个语法错误。我玩了一下。可以提供更多的代码,如果需要的话。
我理解这个问题,但它只是语法...
很多感谢任何帮助。谢谢
更新:一些进展......
},
系列:[{
pointInterval:<%= 1.day * 1000%> ;,
pointStart:<%= 3.weeks.ago.at_midnight.to_i * 1000%> ,
名称:'难度',
数据:<%= Homework.get_difficulty.to_json%>
},
在我的homework.rb模型中:
def self.get_difficulty
HomeworkStudent.pluck(:difficulty)
end
这产生:
但是,这显然是所有记录,而我只希望当前用户显示,并且我不知道如何正确设置日期(updated_at属性)。
我认为您的SQL错误,请尝试homework_id in(?)
c>> homework_id =(?)来选择具有多个值的行。
您的代码应该是:
def student_feedback
difficulty_ids = current_user.school_user.homework_students.pluck(:难度)
@homeworks = HomeworkStudent.where([homework_id in(?),difficulty_ids])。first
end
I am trying to render data on to a spline chart by collecting an array of id's:
Controller
def student_feedback
difficulty_ids = current_user.school_user.homework_students.pluck(:difficulty)
@homeworks = HomeworkStudent.where("homework_id = (?)", difficulty_ids).first
end
The table I am targeting is called homework_students:
create_table "homework_students", force: :cascade do |t|
t.integer "school_user_id", limit: 4, null: false
t.integer "homework_id", limit: 4, null: false
t.datetime "completed_on"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "completed", limit: 1
t.integer "difficulty", limit: 4
t.integer "interest", limit: 4
end
Each student has many different homeworks and they can assign a difficulty value in the difficulty column for each. I am looking to graph this data for each student.
So in the above controller action I am plucking difficulty figures (1 - 5) for each piece of homework the current user is associated with in an array.
In my chart:
...
}
},
series: [{
name: 'Difficulty',
data: [<%= @homeworks.to_json %>]
}]
});
I tried changing action to this:
def student_feedback
difficulty_ids = current_user.school_user.homework_students.pluck(:difficulty)
@homeworks = HomeworkStudent.where(["homework_id = ?", difficulty_ids]).first
end
I keep getting this error:
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
The data being collected is correct. Though it says "homework_id = 4,2.." (this is not correct, those are the difficulty values). I suspect this is just a syntax error. I have toyed around a little bit. Can provide more code if needed.
I understand the problem but it's just the syntax...
Much appreciate any help. Thanks
UPDATE: Some progress...
},
series: [{
pointInterval: <%= 1.day * 1000 %>,
pointStart: <%= 3.weeks.ago.at_midnight.to_i * 1000 %>,
name: 'Difficulty',
data: <%= Homework.get_difficulty.to_json %>
},
In my homework.rb model:
def self.get_difficulty
HomeworkStudent.pluck(:difficulty)
end
This produces:
But this is obviously for all records whereas I only want current user's to show and I don't know how to get the dates set up properly (updated_at attribute).
I think your SQL is wrong, try "homework_id in (?)"
instead of homework_id = (?)
, to select rows with multiple values.
Your code should be:
def student_feedback
difficulty_ids = current_user.school_user.homework_students.pluck(:difficulty)
@homeworks = HomeworkStudent.where(["homework_id in (?)", difficulty_ids]).first
end
这篇关于MySQL,Highcharts:“操作数应该包含1列:”错误(后续)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!