Ok I think I have two problems which need to be fixed
I've got the following tables:
Student
, Register
and a join table which displays the records that 1 register has many students in it, called 'Registers_Students' which looks like this: .
I had to create the Register_Students
table via rails g migration CreateStudentRegister
, which looked like this:
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
Now, I want every register to have a number of students, and for every student, I want them to have a present
status of true/false, and every student should also have a time_of_arrival
time.
However, I have no way of accessing the time_of_arrival
or present
attributes, as I want to set them.
The way I want to change these attributes for every student, is in the register/show.html.erb
using the following code:
<% @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>
The reason I want it to be displayed in the show
page, is so that the register can be edited with marking the student as either being present or absent, and also mark their time of arrival using a checkbox (That part hasn't been implemented yet, but if any of you guys know how to implement it, I'd love to hear about it).
Thanks guys for any answers in advance
EDIT: Added models
Model for 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
And this is the other model for the 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