0

例として、患者との多くの予約を持つ医師がいます。予定で現在アクティブなすべての患者を取得したい:

class Doctor < ActiveRecord::Base
  attr_accessible :name
  has_many :appointments
  has_many :patients, through: :appointments
end

そして予定モデル:

class Appointment < ActiveRecord::Base
  attr_accessible :patient_id, :began_at, :finished_at, :doctor_id
  belongs_to :patient
  belongs_to :doctor
  scope :active, where(finished_at: nil)
end

doctor.appointments.activeこれで、アクティブな現在の予定を取得するようなことができます。しかし、私はそれらの予約の患者を簡単に取得したいと考えています。とにかく何かをすることはありますdoctor.appointments.active.patientsか?またはdoctor.patients.active、もっと理にかなっています。

Doctor クラスactiveの行にスコープを追加できますか?has_many :patients

4

1 に答える 1