【发布时间】:2014-11-28 15:53:52
【问题描述】:
我正在尝试在我的项目中创建某种条件范围,但不知道如何处理。
我有一个医疗机构,是医生。
class Practice < ActiveRecord::Base
has_many :doctors, -> { where removed_at: nil }
end
class Doctor < ActiveRecord::Base
scope :with_all_doctors, -> {includes(:practice).where.not removed_at: nil}
belongs_to :practice
end
现在我希望能够找到一些实践和所有的医生,以及只有在职医生的实践(removed_at == nil)
我正在尝试实现以下目标:
doctor = Doctor.find(id: params[:id]) #here we are fetching object hierarchy
#with only active doctors of practice
#(doctor.practice.doctors will give only active users)
doctor = Doctor.with_all_doctors.find(id: params[:id]) #here we need to fetch object hierarchy
#with all doctors of practice
#(doctor.practice.doctors will give both active and removed doctors)
我将非常感谢您的解决方案。
【问题讨论】:
标签: ruby-on-rails activerecord