- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
好的,我想我有两个问题需要解决
我有以下表格:Student
、Register
和一个连接表,显示 1 个 register 中有很多学生的记录,称为“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
状态为 true/false,每个学生也应该有一个 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 数据库:回滚
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/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!