【问题标题】:Trying to access attribute in join table Rails尝试访问连接表 Rails 中的属性
【发布时间】:2013-03-09 21:27:46
【问题描述】:

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

我有以下表格: StudentRegister 和一个显示 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_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 页面中的原因是,可以编辑注册表,将学生标记为在场或缺席,并使用复选框标记他们的到达时间(该部分还没有实现,但是如果你们中的任何人知道如何实现它,我很想听听)。

谢谢大家提前回答

编辑:添加模型

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

【问题讨论】:

  • 什么不起作用?如果您输入的图形是最新的,则presenttime_of_arrival 返回nil,而&lt;%= nil %&gt; 不会打印任何内容。
  • 是的,我明白这一点,但我无法手动更新 presenttime_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


【解决方案1】:

更新 您需要设置一个模型来访问 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 关系,以便您可以在视图中使用它们。

您现在将为每个学生拥有一个 student_registers 数组,您可以循环访问并访问当前字段 结束更新

1)

  <td><%= Register.find(@register).present %></td>         #Doesn't work

你已经有一个@register 的实例,所以不需要再去abnd 读取数据库来找到同一个。直接在@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,因此请浏览该网站。

【讨论】:

  • 嘿@jamesw,1) 不起作用,我只是使用代码来澄清这些命令不起作用,因为我收到此错误消息:undefined method 'present' for #&lt;Register:0x007fe8da3c12c8&gt; 。 2和3)给我这个错误:undefined method 'time_of_arrival' for #&lt;Student:0x007fe8dd57e220&gt;
  • 我要做的是通过控制台或网络访问这些属性,没关系,只要我可以访问它们
  • @teenOmar 好的,您需要发布 contyroller 操作以及此过程中涉及的所有模型。我很确定我知道发生了什么(您可能还没有建立一个模型来处理 registers_student 表)。我会更新我的答案。
  • 大概就是这样,我想我还没有模型可以访问它。您需要哪些控制器操作?它们只是模板动作,没有对它们中的任何一个进行任何更改(RegisterStudent)。对于模型,我将编辑我的问题并将其包括在内
  • 当你说 set up your has_many and belongs_to relationships so you can use them in your view 时,我应该如何设置它们(考虑到你说使用 HABTM 是个坏主意。很抱歉打扰,但你能告诉我这些关联应该如何设置吗?在StudentRegisterStudent_Register 模型中?顺便说一句,非常感谢;)
【解决方案2】:

你需要实现has_many :through关联。

请参阅 rails 指南,http://guides.rubyonrails.org/association_basics.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2013-09-18
    相关资源
    最近更新 更多