【发布时间】:2016-10-11 14:57:20
【问题描述】:
如何查询has_many :through 以查看另一侧哪些记录具有空关联? (我使用的是 Rails 5)
class Specialty
has_many :doctor_specialties
has_many :doctor_profiles, through: :doctor_specialties
class DoctorProfile
has_many :doctor_specialties
has_many :specialties, through: :doctor_specialties
class DoctorSpecialty
belongs_to :doctor_profile
belongs_to :specialty
我可以通过枚举Specialty 来做到这一点,但我想在 SQL 查询中做到这一点。
Specialty.includes(:doctor_profiles).all.select{|x| x.doctor_profiles.length == 0 }
【问题讨论】:
标签: ruby-on-rails activerecord ruby-on-rails-5