【问题标题】:How to avoid creating duplicate records in join table with has_many through association?如何通过关联避免使用 has_many 在连接表中创建重复记录?
【发布时间】:2017-07-12 07:10:12
【问题描述】:

我有两个模型PhysicianPatient和一个join模型Appointment,关联如下:

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :appointments
  accepts_nested_attributes_for :patients
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, through: :appointments
  accepts_nested_attributes_for :appointments
end

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

我想在appointment 表中有新的时更新appoinment_data appointment 表中的条目。

所以在 Rails 控制台中:

a = Physician.last
#<Physician id: 1, name: "Hamza", address: "Pune", created_at: "2017-02-22 07:07:10", updated_at: "2017-02-22 07:07:10">

a.update(patients_attributes: [{ name: 'Prajakta', disease: 'Fever', appointments_attributes: [{appointment_data: DateTime.now}]}])
   (0.7ms)  BEGIN
  SQL (0.9ms)  INSERT INTO "patients" ("name", "disease", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "Prajakta"], ["disease", "Fever"], ["created_at", "2017-02-22 08:30:22.321863"], ["updated_at", "2017-02-22 08:30:22.321863"]]
  SQL (0.8ms)  INSERT INTO "appointments" ("appointment_data", "patient_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["appointment_data", "2017-02-22 08:30:22.311198"], ["patient_id", 5], ["created_at", "2017-02-22 08:30:22.326373"], ["updated_at", "2017-02-22 08:30:22.326373"]]
  SQL (0.9ms)  INSERT INTO "appointments" ("physician_id", "patient_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["physician_id", 1], ["patient_id", 5], ["created_at", "2017-02-22 08:30:22.333624"], ["updated_at", "2017-02-22 08:30:22.333624"]]
   (1.2ms)  COMMIT
=> true

它正在appointment 表中创建两个recordsappointment_datapatient_id 的一条记录。其他是physician_idpatient_id

我在这里缺少什么?

【问题讨论】:

    标签: ruby-on-rails ruby postgresql activerecord associations


    【解决方案1】:

    我找到了一种解决方法,方法是放置 accepts_nested_attributes_for。我的联想现在是这样的:

    class Physician < ActiveRecord::Base
      has_many :appointments
      has_many :patients, through: :appointments
      accepts_nested_attributes_for :appointments
    end
    
    class Patient < ActiveRecord::Base
      has_many :appointments
      has_many :physicians, through: :appointments
    end
    
    class Appointment < ActiveRecord::Base
      belongs_to :physician
      belongs_to :patient
      accepts_nested_attributes_for :patients
    end
    

    我在控制台中这样做了:

    a = Physician.last
    a.update(appointment_attributes: [{appointment_data: DateTime.now, patient_attributes: {name: 'Rajesh', disease: 'fever'}}])
    

    它现在只在appointments 表中创建一条记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多