我有一个问题:
@st = exam.students.find(:all)
@st.each do |student|
返回给我一个包含所有学生的数组,但是:
exam.students.each do |student|
返回我一个数组,每个学生 4 次
这是打印
打印
有人对此有想法吗?
更新:
这是我的考试模型:
set_table_name "exam"
set_primary_key "ID_Exam"
belongs_to :questionnaire, :foreign_key => "ID_Questionnaire"
has_many :responses, :foreign_key => "ID_Exam"
has_many :students, :through => :responses, :foreign_key => "ID_Exam", :group => "response.ID_Student"
belongs_to :professor, :foreign_key => "ID_Professor"
has_many :student_exam_times
has_many :exam_halted_students
has_many :exam_paused_students
has_many :answered_questions
这是我的学生模型:
set_table_name "student"
set_primary_key "ID_Student"
has_one :user, :foreign_key => "ID_User"
has_many :group_student, :foreign_key => "ID_Student", :group => "group_student.ID_Group"
has_many :groups, :through => :group_student, :foreign_key => "ID_Group"
has_many :responses, :foreign_key => "ID_Student"
has_many :exams, :through => :responses, :foreign_key => "ID_Exam", :group => "exam.ID_Exam"
has_many :student_exam_times
has_many :exam_halted_students
has_many :exam_paused_students
has_many :marked_questions
has_many :answered_questions
has_many :messages, :order => "viewed ASC, send_at DESC"
更新 2:
这是我的块:
students_exam = exam.students.find(:all)
students_exam.each do |student|
cont=StudentExamTime.find(:first,:conditions => {:student_id => student.id, :exam_id => params[:exam_id].to_i })
bd_time=0
if cont==nil
cont=StudentExamTime.new
else
bd_time=cont.time
end
cont.student_id=student.id
cont.exam_id=params[:exam_id].to_i
cont.time=bd_time + params[:time].to_i
cont.save
end
添加:uniq => true
自
has_many :students, :through => :responses, :foreign_key => "ID_Exam", :group => "response.ID_Student"
在您的考试模型中
您可以在考试模型中发布关系吗?
我最初的感觉是,考试和学生之间的关系比典型的关系更复杂,可以从"DISTINCT"声明中受益。
看起来exam.students
正在返回每个学生的相关考试(甚至是重复的)
而exam.students.find(:all)
则返回唯一学生列表。
您的模型关系和表格设置是什么样的?
我怀疑你的块在里面
exam.students.each do |student|
正在返回 4 名学生的数组。 你也可以发布那个块吗?