【发布时间】:2013-03-09 21:27:46
【问题描述】:
好的,我想我有两个问题需要解决
我有以下表格:
Student、Register 和一个显示 1 个寄存器中有很多学生的记录的连接表,称为“Registers_Students”,如下所示:。
我必须通过rails g migration CreateStudentRegister 创建Register_Students 表,如下所示:
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 或 present 属性,因为我想设置它们。
我想为每个学生更改这些属性的方式是在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
【问题讨论】:
-
什么不起作用?如果您输入的图形是最新的,则
present和time_of_arrival返回nil,而<%= nil %>不会打印任何内容。 -
是的,我明白这一点,但我无法手动更新
present或time_of_arrival属性。即使通过 SQLite 它也不会让我更新它 -
habtm 很少是个好主意 - 请在此处查看我的回答 stackoverflow.com/questions/14243566/rails-habtm-joins/…
标签: ruby-on-rails ruby ruby-on-rails-3 activerecord ruby-on-rails-3.2