好吧,我想我有两个问题需要解决
我有以下表格:
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
现在,我希望每个注册都有一定数量的学生,对于每个学生,我希望他们有 true/false 的 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
页面中的原因是,可以通过将学生标记为在场或缺席来编辑登记册,并使用标记他们的到达时间一个复选框(该部分尚未实现,但如果你们中有人知道如何实现它,我很想听听)。
谢谢大家提前的回答
编辑:添加模型
学生模型
:
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
最佳答案
更新 您需要设置一个模型来访问 registers_students 表
该表名称不遵循 Rails 约定或正常语义,所以我可以建议您
1) 使用
将数据库回滚 1 次迁移(假设该表的创建是最后一次迁移)rake db:rollback
2) 从 db/migrations 文件夹中删除迁移
3) 使用内置的 Rails 模型生成器
rails g model student_register register_id:integer student_id:integer #etc
现在迁移您的数据库,然后设置您的 has_many 和 Belongs_to 关系,以便您可以在 View 中使用它们。
现在,您将为每个学生拥有一个 Student_registers 数组,您可以循环遍历并访问当前字段 结束更新
1)
<td><%= Register.find(@register).present %></td> #Doesn't work
您已经拥有一个 @register 实例,因此无需再次读取数据库来查找相同的实例。只需直接在@register对象上使用你想要的属性即可
<td><%= @register.present %></td>
你的原始代码虽然设计不好,但应该可以工作
如果这不起作用,那么您遇到了一个更根本的问题,无法用您提供的少量信息来猜测该问题,因此请返回并编辑您的问题,如果是这种情况,请留下评论。
2)
同样的道理
<td><%= Register.find(@register).time_of_arrival %></td> #This doesn't work
3)
因为你说这不起作用
<td><%= student.time_of_arrival%></td> #Nor does this
我开始怀疑你的说法不起作用,并认为它工作得很好 也许您在这些字段中没有要显示的数据。因此,输入一些数据,一切都会好的。
4) 使用 Rails 助手在屏幕上编辑多条记录有 3 种主要方法。 我建议你阅读 fields_for
作为起点http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
另请查看此 http://railscasts.com/episodes/198-edit-multiple-individually Ryan Bates 已经就这个主题进行了其他的广播,因此可以通过该网站进行搜索。
关于ruby-on-rails - 尝试访问连接表Rails中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15316114/