MySQL,Highcharts:“操作数应包含1列:”错误(跟进)

时间:2016-12-24 21:55:58

标签: mysql ruby-on-rails highcharts

我试图通过收集id的数组来将数据渲染到样条图表上:

控制器

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_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

每个学生都有许多不同的作业,他们可以在难度栏中分配难度值。我希望为每个学生绘制这些数据。

所以在上面的控制器动作中,我为当前用户在一个数组中关联的每一件作业挑选难度数字(1 - 5)。

在我的图表中:

...
       }
        },
        series: [{
            name: 'Difficulty',
            data: [<%= @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: 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 = 4,2 ..&#34; (这是不正确的,那些是难度值)。我怀疑这只是一个语法错误。我玩了一下。如果需要,可以提供更多代码。

我理解这个问题,但它只是语法......

非常感谢任何帮助。感谢

更新:一些进展......

      },
series: [{
    pointInterval: <%= 1.day * 1000 %>,
    pointStart: <%= 3.weeks.ago.at_midnight.to_i * 1000 %>,
    name: 'Difficulty',
    data: <%= Homework.get_difficulty.to_json %>
     },

在我的homework.rb模型中:

def self.get_difficulty
      HomeworkStudent.pluck(:difficulty)
    end

这会产生:

enter image description here

但这显然适用于所有记录,而我只希望显示当前用户,而且我不知道如何正确设置日期(updated_at属性)。

1 个答案:

答案 0 :(得分:1)

我认为您的SQL错误,请尝试"homework_id in (?)"而不是homework_id = (?),以选择具有多个值的行。

您的代码应为:

def student_feedback
    difficulty_ids = current_user.school_user.homework_students.pluck(:difficulty)
    @homeworks = HomeworkStudent.where(["homework_id in (?)", difficulty_ids]).first
end