以下のように、医師と患者の 2 つのモデルがあります。
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
p = 患者オブジェクト。
したがって、p.appointments.append (physican_obj) が適切な方法です。しかし、同じコマンドを 2 回実行すると、同じものが 2 回追加されます。では、追加する前に既に存在するかどうかを確認するのは良いことですか? それを行う最良の方法は何ですか?
私の要件は、追加するオブジェクトのリストを取得するため、各オブジェクトをループして各オブジェクトを追加する必要があることです。追加する前にチェックする必要がある場合は、コストのかかるプロセスであるすべてのループをチェックインする必要があります。