如果我有以下的協會......Ruby on Rails 3:如何在不刪除數據庫的情況下斷開對象與其他對象的連接?
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
我可以將現有的任命添加到醫生...
appoint = Appointments.find(params[:id])
phys = Physician.find(params[:id])
phys.appointments << appoint
phys.save
,但我無法弄清楚如何刪除約會從醫師名單中刪除而不刪除它。我想在與Physician表斷開連接後將約會保留在約會表中。
非常感謝您的智慧!
這是最簡單的方法。但是從問題來看,他是否想保持參照完整性和/或是否能夠跟蹤一個約會過去曾經聯繫過的醫生。通過將其設置爲空,他會丟失該信息。但也許它確定。這個問題沒有具體說明。 – 2012-02-27 22:14:49