一個問題,我有一個問題有關:的Rails 3:有關數組
@st = exam.students.find(:all)
@st.each do |student|
返回我的數組與所有的學生,但:
exam.students.each do |student|
返回我的陣列,4次,每次學生
這裏是一個打印
任何人都有這個想法?
UPDATE:
這裏是我的考試模式:
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
這是奇怪的,這是什麼'exam.students.count'說? – sled 2011-05-17 18:49:01
嗨,thx爲您的迴應。 exam.students.count返回12和3 for exam.students.find(:all) – 2011-05-17 19:11:28
您好,您可以在檢查模型中發佈'has_many'關聯嗎?並用mysql管理控制檯檢查你的數據庫表。 – sled 2011-05-17 19:17:56