我有一个DB存储学生对测验问题的答案。这些与学生学习成果相关,我想通过下面描述的一系列关系进行审查。我试图计算正确回答了多少个问题,并为每个学生的学习成果错误地回答了多少个问题,但我仍然收到错误。预先感谢您提供的任何帮助。Rails与has_many通过关系嵌套连接
一般的映射是
“答案”回答了这个测试,涵盖许多“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, “答案” “is_correct”,计数( answers.id)AS total_answers从 “answers”INNER JOIN“knowledge_topics”ON“knowledge_topics”。“id” =“answers”。“KnowledgeTopic_id”INNER JOIN“kt_covered_by_slos”ON“kt_covered_by_slos”。“knowledge_topic_id”=“knowledge_topics” 。“id”
INNER JOIN“student_learning_outcomes”ON。“student_learning_outcomes”, “ID”= “kt_covered_by_slos” “StudentLearningOutcome_id” GROUP BY 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
运行查询您可以使用'Answer','KtCoveredBySlo'进行迁移来更新您的问题吗?您的查询为'KnowledgeTopic'的外键显示两个不同的列名。 – vee 2014-11-25 14:29:56
你还可以请你以更容易阅读的方式来构建你的查询,比如把它分成几行。 – 2014-11-25 14:32:15
您是否试图遵循rails惯例,并在模型中的迁移和关联声明中使用snake_case命名,或者它不是一个选项? – ave 2014-11-25 15:00:40