从 has_one 关联中调用一个值

Calling a value from has_one association

我正在尝试从 has_one 协会获取一些信息,但我无法获得正确的调用方式。我有 2 table 个学生 table,其中 has_one emergency_contact。 emergency_contact table 有一个外键值 student_id。我正在尝试 link_to 在 emergency_contact table 中附加外键的记录,但我似乎无法正确理解它。

student.rb

class Student < ApplicationRecord
self.primary_key = :student_id
has_one :emergency_contact

emergency_contact.rb

class EmergencyContact < ApplicationRecord
self.primary_key = :contact_id
belongs_to  :student, optional: true

students/show.html.erb

<%= emergency_contact_path(@student.student_id) %>

现在我只是被定向到 student_id 的值,例如 student_id = 5。当 emergency_contact.contact_id 的 contact_id = 1 .我怎样才能得到那个1?

has_one 设置 foreign_key:

has_one :emergency_contact, foreign_key: 'contact_id'

Link 是:

<%= emergency_contact_path(@student.emergency_contact.contact_id) %>