Search code examples
ruby-on-railsruby-on-rails-2

Query execute in N+1 times


In my application i am using

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]}

code. When i run the code it executes N+1 times. I am sure this is the problem of N+1 query in rails. I need to simplify this code with out N+1.

Note : clinician_affiliation is belongs to provider_organization. (I am using Rails 2.3)

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

From DB Physician and Provider_organizations are types of party table.

class Physician < Clinician


Solution

  • I think this should work

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