Rails Rich Association - 覆盖关联模型

发布于 2024-11-19 16:37:10 字数 1355 浏览 6 评论 0原文

假设我们有三个模型 PATIENTS、NURSES 及其关联模型 APPOINTMENTS。

本周所有患者都必须与护士会面。

一个病人有很多护士,一个护士有很多病人。同样,一名患者有很多预约,但每个护士只有一次,而一名护士有很多预约,但每个患者只有一次。

class Patient < ActiveRecord::Base 
  has_many :appointments
  has_many :nurses, :through => :appointments
end


class Nurse < ActiveRecord::Base 
  has_many :appointments
  has_many :patients, :through => :appointments
end


class Appointment < ActiveRecord::Base
  belongs_to :patient
  belongs_to :nurse
end

显然,预约既属于患者又属于护士。

然而,我还想要一种方法让护士能够勾选病人是否出现。我在迁移中实现了这一点,如下所示:

class CreateAppointments < ActiveRecord::Migration
  def self.up
    create_table :appointments do |t|
      t.references :patient
      t.references :nurse
      t.boolean "present", :default => false
      t.timestamps
    end
    add_index :job_enrollments, ['patient_id', 'nurse_id']
  end

  def self.down
    drop_table :appointments
  end

end

在控制台中,我可以创建所有内容的实例,并且可以执行以下操作:

appt = Appointment.new
patient = Patient.new
nurse = Nurse.new

patient.appointments << appt
nurse.appointments << appt

现在,问题来了:我如何实现它,以便该预约中的护士能够将布尔值编辑为 TRUE?到目前为止,我可以通过键入以下内容来更改约会实例的值:

appt.present = true

但这不是我想要的。我希望护士能够修改这种关联,并阻止患者进行任何更改。

我不应该为此使用丰富的关联吗?

Suppose we have three models PATIENTS, NURSES, and their association model APPOINTMENTS.

This week all patients have to meet with their nurses.

A patient has many nurses, and a nurse has many patients. Likewise, a patient has many appointments, but only one per nurse, and a nurse has many appointments, but only one per patient.

class Patient < ActiveRecord::Base 
  has_many :appointments
  has_many :nurses, :through => :appointments
end


class Nurse < ActiveRecord::Base 
  has_many :appointments
  has_many :patients, :through => :appointments
end


class Appointment < ActiveRecord::Base
  belongs_to :patient
  belongs_to :nurse
end

Obviously, the APPOINTMENT belongs to both the patient and the nurse.

However, I also want a way for the nurse to be able to tick off whether or not the patient showed up. I implemented this within the migration like such:

class CreateAppointments < ActiveRecord::Migration
  def self.up
    create_table :appointments do |t|
      t.references :patient
      t.references :nurse
      t.boolean "present", :default => false
      t.timestamps
    end
    add_index :job_enrollments, ['patient_id', 'nurse_id']
  end

  def self.down
    drop_table :appointments
  end

end

In the console, I could create an instance of everything, and could do the following:

appt = Appointment.new
patient = Patient.new
nurse = Nurse.new

patient.appointments << appt
nurse.appointments << appt

Now, here comes the question: How do I implement it so that the Nurse in that appointment is able to edit the value of the boolean to TRUE? So far, I can change the value of the instance of the appointment by typing:

appt.present = true

But this not what I want. I want the nurse to be able to modify this association, and lock out the patient from changing anything.

Should I simply not be using rich associations for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极度宠爱 2024-11-26 16:37:10

我假设您的问题是关于如何使用这些关联。保留预约后,您可以调用和修改预约记录,如下所示:

# current_nurse is the nurse using the system and reporting
appt = current_nurse.appointments.where(:patient_id => params[:patient_id]).first
appt.update_attribute :present, true

为了防止患者访问和修改预约,您需要一个身份验证机制,例如 设计。然后仅授予护士访问系统的权限。

I'm assuming your question is about how to use these associations. You can recall and modify the appointment record after the appointment is kept, like so:

# current_nurse is the nurse using the system and reporting
appt = current_nurse.appointments.where(:patient_id => params[:patient_id]).first
appt.update_attribute :present, true

To prevent the patient from accessing and modifying the appointment, you would need an authentication mechanism, such as devise. Then only grant Nurses permission to access the system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文