Ruby on rails Rails嵌套连接具有多个直通关系
我有一个数据库,存储学生对测验问题的答案。这些都与学生的学习成果有关,我想通过下面描述的一系列关系来回顾这些成果。我试图计算每个学生的学习结果中有多少问题回答正确,有多少问题回答错误,但我不断收到错误。提前感谢您提供的任何帮助 一般的映射是 “答案”回答了一个测试“知识主题”的问题,该主题涵盖了许多(“KtCoveredBySLO”)“学生学习结果” 我的型号具有以下关系:Ruby on rails Rails嵌套连接具有多个直通关系,ruby-on-rails,activerecord,has-many,Ruby On Rails,Activerecord,Has Many,我有一个数据库,存储学生对测验问题的答案。这些都与学生的学习成果有关,我想通过下面描述的一系列关系来回顾这些成果。我试图计算每个学生的学习结果中有多少问题回答正确,有多少问题回答错误,但我不断收到错误。提前感谢您提供的任何帮助 一般的映射是 “答案”回答了一个测试“知识主题”的问题,该主题涵盖了许多(“KtCoveredBySLO”)“学生学习结果” 我的型号具有以下关系: class Answer < ActiveRecord::Base belongs_to :KnowledgeT
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
我收到的错误是:
SQLite3::SQLException:没有这样的列:
kt\u由\u slos覆盖\u知识\u主题\u id:选择
学生学习结果id作为学生学习结果id,
“答案”“是否正确”,将(answers.id)计算为来自的总答案
回答“知识主题”上的“内部连接”知识主题。“id”
=“答案”。“知识主题id”在“知识主题id”上内部连接“知识主题id”。“知识主题id”=“知识主题id”。“id”上的内部连接“学生学习成果” “学生学习成果”。“id”= “kt由SLO覆盖”,“学生学习结果”分组由 学生学习结果id是否正确 迁移:
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
class CreateKtCoveredBySlos谢谢大家。使用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)
您能否使用迁移更新您的问题,以获得答案
,KtCoveredBySlo
?您的查询为KnowledgeTopic
的外键显示了两个不同的列名。您能否以更易于阅读的方式组织您的查询,比如把它分成几行。你有没有尝试过遵循rails的惯例,在模型中的迁移和关联声明中使用snake_大小写命名,或者这不是一个选项?ave-我想我是基于guides.rubyOnRails.org上的ruby指南。snake-case属性和表名,但在表中使用单个条目时使用camal-case。这是不对的吗?有没有我把事情搞砸的地方?@KatieT,不,这是不对的。类和模块名只能(应该)使用camel大小写。我建议你读一读
@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)