好的,我想我有两个问题需要解决
我有以下表格:
Student
和Register
一个显示 1 个寄存器中有很多学生的记录的连接表,称为“Registers_Students”,如下所示:。
我必须通过创建Register_Students
表rails g migration CreateStudentRegister
,如下所示:
class CreateStudentRegister < ActiveRecord::Migration
def change
create_table :registers_students, :id => false do |t|
t.integer :register_id
t.integer :student_id
t.boolean :present
t.time :time_of_arrival
end
end
end
现在,我希望每个寄存器都有一些学生,对于每个学生,我希望他们有一个present
真/假状态,每个学生也应该有一个time_of_arrival
时间。但是,我无法访问time_of_arrival
orpresent
属性,因为我想设置它们。
我想为每个学生更改这些属性的方法是register/show.html.erb
使用以下代码:
<% @register.students.each do |student| %>
<tr>
<td><%= Register.find(@register).present %></td> #Doesn't work
<td><%= student.university_id%></td>
<td><%= student.first_name.titlecase%></td>
<td><%= student.last_name.titlecase%></td>
<td><%= Register.find(@register).time_of_arrival %></td> #This doesn't work
<td><%= student.time_of_arrival%></td> #Nor does this
</tr>
<% end %>
</table>
我希望它显示在show
页面中的原因是,可以编辑登记册,将学生标记为在场或缺席,并使用复选框标记他们的到达时间(该部分尚未实施,但如果你们中的任何人知道如何实现它,我很想听听)。
谢谢大家提前回答
编辑:添加模型
型号Students
:
class Student < ActiveRecord::Base
has_and_belongs_to_many :registers
validates :university_id, :length => { :minimum => 10}, #Checks that the University ID is 10 characters long
:uniqueness => {:case_sensitive => false}, #Checks that the University ID is unique, regardless of case-sensitivity
:format => { :with => /[wW]\d\d\d\d\d\d\d\d\d/, #University ID needs to be in the right format via regex
:message => "Needs to be in the right format i.e. w123456789"}
default_scope :order => 'LOWER(last_name)' #Orders the students via last name
attr_accessible :first_name, :last_name, :university_id
def name
first_name.titlecase + " " + last_name.titlecase
end
end
这是另一个模型Register
class Register < ActiveRecord::Base
has_and_belongs_to_many :students
belongs_to :event
attr_accessible :date, :student_ids, :module_class_id, :event_id
end