简体   繁体   中英

Rails querying many-to-many

Quick question here. Given the following example many-to-many relationship, how would I query the Physician table for appointments they have today?

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

On the Physician model I have the following:

scope :for, -> (name) { find_by_name(name: name) }
# I need a hand here, the join I assumed would work didn't seem to filter properly.
# scope :appointments_today, -> { joins(:appointment).where("appointments.appointment_date = ?", Date.today) }
scope :appointments_today, -> { ??? }

I'd like to chain queries on the controller as such:

data = Physician.for("test").appointments_today

If you are trying to fetch a list of appointments, the logic should go in the Appointment model:

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

  scope :for_today, -> { where('appointments.appointment_date >= ? AND appointments.appointment_date < ?', Time.zone.now.beginning_of_day, Time.zone.now.end_of_day) }

  scope :for_physician, -> (name) { joins(:physician).where(physicians: {name: name}) }
end

And the you can find the appointments by:

data = Appointment.for_today.for_physician("test")

Try to do this

scope :for, -> (name) { where(name: name) }

Next, you have to add plural into appointments

scope :appointments_today, -> { joins(:appointments).where("appointments.appointment_date = ?", Date.today) }

I hope this help you.

Do you want a list of Physician records that have an appointment scheduled for today, or do you want a list of Appointment records that are for a specific physician and are scheduled for today?

Physicians that have an appointment today:

Physician.
  joins(:appointments).
  where(
    name: "test",
    appointments: {
      appointment_date: (Date.today.beginning_of_day..Date.today.end_of_day)
    }
  )

Appointments for a physician that are today:

Appointment.
  joins(:physician).
  where(
    appointment_date: (Date.today.beginning_of_day..Date.today.end_of_day),
    physicians: { name: "test" }
  )

As scopes, you can do physicians that have an appointment today:

class Physician < ActiveRecord::Base
  scope :named, -> (name) { where(name: name) }
  scope :with_appointments_on, -> (date) { joins(:appointments).where(appointments: { appointment_date: (date.beginning_of_day..date.end_of_day) })}
end

Physician.named("test").with_appointments_on(Date.today)

Or appointments for a physician that are today:

class Appointment < ActiveRecord::Base
  scope :on_date, -> (date) { where(appointment_date: (date.beginning_of_day..date.end_of_day)) }
end

Physician.find_by_name("test").appointments.on_date(Date.today)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM