gpt4 book ai didi

ruby-on-rails - rails 5 has_many through order on through table

转载 作者:行者123 更新时间:2023-12-03 16:14:42 24 4
gpt4 key购买 nike

我要订购一个 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

我想按专栏订购医生专业 ordinalDoctorSpecialty .特别是在使用 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

并收到一个丢失的 FROM 子句错误 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

关于ruby-on-rails - rails 5 has_many through order on through table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40247388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com