我有一個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