加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – rails 5 has_many通过表顺序

发布时间:2020-12-17 01:57:03 所属栏目:百科 来源:网络整理
导读:我想在直通表中的列上订购has_many through关系 class DoctorProfile has_many :doctor_specialties has_many :specialties,through: :doctor_specialtiesclass Specialty has_many :doctor_specialties has_many :doctor_profiles,through: :doctor_special
我想在直通表中的列上订购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

我想在DoctorSpecialty的专栏上订购医生专业.特别是在使用includes时会发生此错误

DoctorProfile.includes(:专科).所有

我试过了

has_many:特色,– > {order’Soctor_specialties.ordinal’},通过:: 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 -clause错误PG :: UndefinedTable:错误:缺少表“doctor_specialties”的FROM子句条目

如何在直通表上定义订单,以便专业按升序返回?

注意:

我能够通过向DoctorSpecialty添加default_scope来实现这一目标

default_scope {order(‘ordinal ASC’)}

但是,我仍然想知道是否有办法在has_many上执行此操作

解决方法

不确定这是否是导致您的错误的原因,但您还没有完成在专业方面有很多关系.应该是has_many:doctor_profiles,通过:: doctor_specialties

另外对于DoctorProfiles中的这一行has_many:专业,通过:doctor_specialties,doctor_specialties需要成为一个符号

至于排序,我认为你需要做一个连接而不是包含

像DoctorProfile.joins(:doctor_specialties).order(“doctor_specialties.ordinal ASC”)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读