2014-11-28 2 views
0

프로젝트에서 조건부 범위 지정을 만들려고하고 있지만이를 처리하는 방법을 알 수 없습니다.ActiveRecord 조건부 범위

저는 의사의 진료를 받았으며 의사입니다.

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 == 전무) 내가 좋아하는 뭔가를 달성하기 위해 노력하고있어

을 가진 의사뿐만 아니라 연습입니다 :

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) 

나는 해결책에 대해 매우 감사 할 것입니다.

답변

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 모든 의사를 얻을하고 할 수 있습니다.

+0

실생활에서는 개체 계층 구조가 더 깊어서 수동으로 작성하고 싶지 않습니다. 나는 올바른 구조를 얻고 클라이언트의 응용 프로그램으로 json으로 보내야합니다. – TopaZ

+0

당신이 계층 구조의 "그림"을 우리에게 제공한다면 우리는 그것을위한 해결책을 제안 할 수 있습니다. – sebkkom

+0

데모 목적으로 제공된 구조로 충분하다고 생각합니다. (NDA로 인해 더 많이 노출시킬 수는 없습니다). 모델 객체를 직접 호출하지 않고도 원하는 구조를 얻을 수있는 방법이 필요합니다. – TopaZ

관련 문제