我希望能够使用includes
我的模特:
DoctorProfile
has_many :doctor_specialties
has_many :specialties, -> { order 'doctor_specialties.ordinal' }, through: :doctor_specialties
当我尝试查询DoctorProfile.includes(:specialties).all
时,收到以下错误:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "doctor_specialties"
LINE 1: ...WHERE "specialties"."id" IN (91, 69, 72) ORDER BY doctor_spe...
^
: SELECT "specialties".* FROM "specialties" WHERE "specialties"."id" IN (91, 69, 72) ORDER BY doctor_specialties.ordinal
但是,当我删除订单条款时,我能够使用上述查询。
如何在维护订单时使用include? p>
答案 0 :(得分:0)
如果您加入doctor_specialities
表,它是否有效,如下所示:
has_many :specialties, -> { joins(:doctor_specialities).order('doctor_specialties.ordinal') }, through: :doctor_specialties
这是有意义的,这是一个常规的查询/范围,所以我认为它应该在这里工作。
让我知道你是怎么过的。