2013-05-28 71 views
0

在我的申請我使用查詢執行在N + 1次

phy = Physician.find(:all, :include => {:clinician_affiliations => :provider_organization}, :with_disabled => true).select{|physician| not physician.provider_organizations.blank? }.collect{|enum| [enum.display_name_schedule, enum.id]} 

代碼。當我運行代碼時,它執行N + 1次。我確信這是在rails中N + 1查詢的問題。我需要用N + 1來簡化這個代碼。

注意:clinicalician_affiliation屬於provider_organization。 (我正在使用Rails 2.3)

class Clinician 
..... 
has_many :provider_organizations, :through => :clinician_affiliations 
... 
end 

來自DB Physician和Provider_organizations是派對錶類型。

類醫師<臨牀醫生

+0

什麼是醫生和provider_organization之間的關係? – 2013-05-28 11:08:48

+0

@santhosh我更新了我的問題.. – Bathakarai

回答

1

我認爲這應該工作

Physician.find(:all, :include => :provider_organizations) 
+0

請看看http://stackoverflow.com/questions/16806038/spliting-large-statement-into-subpart這個 – Bathakarai

+0

@Bathakarai有一個答案,它似乎是正確的對我來說。 – 2013-05-29 06:11:05