Rails 3:关于数组的问题

时间:2011-05-17 18:45:54

标签: ruby-on-rails ruby ruby-on-rails-3

我对此有疑问:

@st = exam.students.find(:all)
@st.each do |student|

给我所有学生一个阵列,但是:

exam.students.each do |student|

给我一​​个每个学生4次的阵列

这是一个打印

print

有人对此有所了解吗?

更新:

这是我的考试模型:

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

4 个答案:

答案 0 :(得分:1)

添加:uniq => true

has_many :students, :through => :responses, :foreign_key => "ID_Exam", :group => "response.ID_Student"

在考试模型中

答案 1 :(得分:0)

您可以在考试模型中发布关系吗?

我最初的感觉是,考试和学生之间的关系比典型的更复杂,可以从“DISTINCT”声明中受益。

答案 2 :(得分:0)

看起来exam.students正在返回每个有相关考试的学生(甚至重复)

exam.students.find(:all)正在返回一份独特学生名单。

你的模型关系是什么样的,你的表设置是什么?

答案 3 :(得分:0)

我怀疑你的阻碍

exam.students.each do |student|

返回一个由4名学生组成的数组。你也可以发布那个街区。