gpt4 book ai didi

ruby-on-rails - 尝试访问连接表 Rails 中的属性

转载 作者:数据小太阳 更新时间:2023-10-29 08:39:11 26 4
gpt4 key购买 nike

好的,我想我有两个问题需要解决

我有以下表格:StudentRegister 和一个连接表,显示 1 个 register 中有很多学生的记录,称为“Registers_Students”,如下所示:enter image description here

我必须通过 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 状态为 true/false,每个学生也应该有一个 time_of_arrival 时间。但是,我无法访问 time_of_arrivalpresent 属性,因为我想设置它们。

我想为每个学生更改这些属性的方法是在 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 数据库:回滚

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) 有 3 种主要方法可以使用 Rails 助手在屏幕上编辑多条记录。我建议你阅读 fields_for

作为起点 http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

另请查看此 http://railscasts.com/episodes/198-edit-multiple-individually瑞安·贝茨 (Ryan Bates) 就此主题做过其他 railscast,因此请浏览该网站。

关于ruby-on-rails - 尝试访问连接表 Rails 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15316114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com