我有一个DB,可以存储学生对测验问题的答案。这些与学生学习成果有关,我想通过下面描述的一系列关系进行审查。我正在尝试计算正确回答了多少问题以及每个学生学习结果错误地回答了多少问题,但我一直收到错误。提前感谢您提供的任何帮助。
一般映射是
“答案”回答了一个问题,该问题测试了涵盖许多人的“KnowledgeTopic”(“KtCoveredBySLO”)“StudentLearningOutcome”
我的模型具有以下关系:
class Answer < ActiveRecord::Base
belongs_to :KnowledgeTopic
end
class KnowledgeTopic < ActiveRecord::Base
has_many :answers
has_many :kt_covered_by_slos
has_many :student_learning_outcomes, through: :kt_covered_by_slos
end
class KtCoveredBySlo < ActiveRecord::Base
belongs_to :StudentLearningOutcome
belongs_to :KnowledgeTopic
end
class StudentLearningOutcome < ActiveRecord::Base
has_many :kt_covered_by_slos
has_many :knowledge_topics, through: :kt_covered_by_slos
end
我试图运行的查询是:
@answers = Answer.joins(:KnowledgeTopic => {:kt_covered_by_slos => :StudentLearningOutcome})
.select( 'student_learning_outcomes.id As student_learning_outcomes_id',
:is_correct, "count(answers.id) AS total_answers")
.group('student_learning_outcomes.id', :is_correct)
我收到的错误是:
SQLite3 :: SQLException:没有这样的列: kt_covered_by_slos.knowledge_topic_id:SELECT student_learning_outcomes.id作为student_learning_outcomes_id, &#34;答案&#34;。&#34; is_correct&#34;,count(answers.id)AS total_answers FROM &#34;答案&#34; INNER JOIN&#34; knowledge_topics&#34; ON&#34; knowledge_topics&#34;。&#34; id&#34; =&#34;答案&#34;。&#34; KnowledgeTopic_id&#34; INNER JOIN&#34; kt_covered_by_slos&#34; ON&#34; kt_covered_by_slos&#34;。&#34; knowledge_topic_id&#34; =&#34; knowledge_topics&#34;。&#34; id&#34;
INNER JOIN&#34; student_learning_outcomes&#34;上 &#34; student_learning_outcomes&#34;&#34; ID&#34; = &#34; kt_covered_by_slos&#34;&#34; StudentLearningOutcome_id&#34;通过...分组 student_learning_outcomes.id,is_correct
迁移
class CreateKtCoveredBySlos < ActiveRecord::Migration
def change
create_table :kt_covered_by_slos do |t|
t.references :StudentLearningOutcome, index: true
t.references :KnowledgeTopic, index: true
t.timestamps
end
end
end
class CreateAnswers < ActiveRecord::Migration
def change
create_table :answers do |t|
t.references :Question, index: true
t.references :Section, index: true
t.references :Student, index: true
t.references :KnowledgeTopic, index: true
t.boolean :is_correct
t.string :answer_text
t.references :Enroll, index: true
t.timestamps
end
end
end
class CreateKnowledgeTopics < ActiveRecord::Migration
def change
create_table :knowledge_topics do |t|
t.string :knowledge_area
t.string :knowledge_unit
t.string :knowledge_topic
t.integer :year_added
t.boolean :active
t.integer :correct_answers
t.integer :incorrect_answers
t.integer :temp_correct_answer
t.integer :temp_incorrect_answer
t.timestamps
end
end
end
class CreateStudentLearningOutcomes < ActiveRecord::Migration
def change
create_table :student_learning_outcomes do |t|
t.string :accredidation_body
t.string :title
t.string :description
t.integer :year_added
t.boolean :active
t.integer :correct_answers
t.integer :incorrect_answers
t.integer :temp_correct_answer
t.integer :temp_incorrect_answer
t.timestamps
end
end
end
答案 0 :(得分:1)
谢谢大家。使用Ave的解释,我更改了引用名称,然后相应地更新了其他所有内容。之后我能够用
运行查询@answers = Answer.joins(knowledge_topic: :student_learning_outcomes)
.select( 'student_learning_outcomes.id As student_learning_outcomes_id', :is_correct,
"count(answers.id) AS total_answers")
.group('student_learning_outcomes.id', :is_correct)