简体   繁体   English

Rails 5 has_many通过表的顺序

[英]rails 5 has_many through order on through table

I want to order a has_many through relationship on a column in the through table 我想在通过表中的列上订购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

I'd like the doctor specialties to be ordered by the column ordinal on DoctorSpecialty . 我希望按DoctorSpecialty ordinal订购医生专科。 Specifically this error happens when using includes 具体来说,使用includes时会发生此错误

DoctorProfile.includes(:specialties).all

I've tried 我试过了

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

and receieve a missing FROM -clause error PG::UndefinedTable: ERROR: missing FROM-clause entry for table "doctor_specialties" 并接收到缺少的FROM PG::UndefinedTable: ERROR: missing FROM-clause entry for table "doctor_specialties"错误PG::UndefinedTable: ERROR: missing FROM-clause entry for table "doctor_specialties"

How can I define the order on the through table so specialties are returning in ascending order? 我如何在通过表上定义顺序,以便专业按升序返回?

Note: 注意:

I was able to get this working by adding a default_scope to DoctorSpecialty 我可以通过在DoctorSpecialty添加default_scope使此工作DoctorSpecialty

default_scope { order('ordinal ASC') }

However, I'm still wondering if there is a way to do it on the has_many through 但是,我仍然想知道是否有办法在has_many through

I was able to get it working using 我能够使用它来工作

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

Not sure if this is what's causing your error but you haven't completed the has many through relationship on the Specialty side. 不知道这是否是导致您出错的原因,但您尚未完成“专业”方面的“通过”关系。 Should be has_many :doctor_profiles, through: :doctor_specialties 应该是has_many :doctor_profiles, through: :doctor_specialties

Also for this line in DoctorProfiles has_many :specialties, through: doctor_specialties , doctor_specialties needs to be a symbol 同样对于DoctorProfiles中的这一行has_many :specialties, through: doctor_specialtiesdoctor_specialties必须是一个符号

As for the ordering I think you need to do a joins instead of an includes 至于排序,我认为您需要进行joins而不是includes

like DoctorProfile.joins(:doctor_specialties).order("doctor_specialties.ordinal ASC") DoctorProfile.joins(:doctor_specialties).order("doctor_specialties.ordinal ASC")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM