use*_*587 7 activerecord ruby-on-rails
我想has_many through在通过表中的列上订购关系
class DoctorProfile
has_many :doctor_specialties
has_many :specialties, through: :doctor_specialties
class Specialty
has_many :doctor_specialties
has_many :doctor_profiles, through: :doctor_specialties
class DoctorSpecialty
belongs_to :doctor_profile
belongs_to :specialty
Run Code Online (Sandbox Code Playgroud)
我想医生专业被列进行排序ordinal的DoctorSpecialty。具体来说,使用时会发生此错误includes
DoctorProfile.includes(:specialties).all
我试过了
has_many :specialties, -> { order 'doctor_specialties.ordinal' }, through: :doctor_specialties
DoctorProfile Load (0.6ms) SELECT "doctor_profiles".* FROM "doctor_profiles" ORDER BY "doctor_profiles"."id" ASC LIMIT $1 [["LIMIT", 1]]
DoctorSpecialty Load (0.8ms) SELECT "doctor_specialties".* FROM "doctor_specialties" WHERE "doctor_specialties"."doctor_profile_id" = 1
Specialty Load (0.4ms) SELECT "specialties".* FROM "specialties" WHERE "specialties"."id" = 69 ORDER BY doctor_specialties.ordinal
Run Code Online (Sandbox Code Playgroud)
并收到缺少的FROM -clause错误 PG::UndefinedTable: ERROR: missing FROM-clause entry for table "doctor_specialties"
我如何在通过表上定义顺序,以便专业按升序返回?
注意:
我可以通过添加一个default_scope来使此工作DoctorSpecialty
default_scope { order('ordinal ASC') }
但是,我仍然想知道是否有办法在 has_many through
我能够使用它来工作
class DoctorProfile
has_many :specialties, -> { order 'doctor_specialties.ordinal' }, through: :doctor_specialties
end
class DoctorSpecialty < ApplicationRecord
belongs_to :doctor_profile
belongs_to :specialty
default_scope { order('ordinal ASC') }
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1489 次 |
| 最近记录: |