I have two models, Appointment
and Teacher
. Teacher
is a sub-class of User
. I want to have a controller return a scope for Appointment
but join and includes with Teacher
so that I can return all of the data from that table. However, either the data will only be from the Appointment
scope or will give me the error Object doesn't support #inspect
, primarily when I use the includes
or select
methods after joins
. Did I make a mistake somewhere or is this just not possible?
class Appointment < ApplicationRecord
belongs_to :teacher, class_name: "User"
belongs_to :student, class_name: "User", optional: true
validates :start_datetime, uniqueness: true
scope :all_available_appts, -> { where(status: "Available") }
scope :all_available_appts_for_date, ->(date) { all_available_appts.where(start_datetime: date.to_date.beginning_of_day..date.to_date.end_of_day) }
end
class Teacher < User
has_many :appointments
end
class Student < User
has_many :appointments
end
def available_appointments_for_date
student = Student.find_by(id: params[:student_id])
if student
render json: {
appointments: Appointment.all_available_appts_for_date(params[:date]).joins(:coach).includes(:first_name, :last_name)
# appointments: Appointment.eager_load(:users).all_available_appts_for_date(params[:date]).select('users.*, appointments.*')
}
else
render json: {
status: 500,
error: "Student or appointments not found"
}
end
end