我正在尝试在我的项目中创建某种条件范围,但无法弄清楚如何处理它。
我有医疗实践,而且是医生。
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)
我非常感谢解决方案。
答案 0 :(得分:2)
我认为你的情况很复杂。为什么不这样呢?
class Practice < ActiveRecord::Base
has_many :doctors
end
class Doctor < ActiveRecord::Base
belongs_to :practice
scope :active, -> { where :removed_at => nil }
scope :inactive, -> { where("removed_at is not null") }
end
这样,你可以docs = Practice.find(params[:id]).doctors
让所有医生,然后docs.active
或docs.inactive
根据您的需要为活动和非活动医生。