문제

In a Rails 2.3.5 application I've got something like the following models:

class Foo < ActiveRecord::Base
  has_many :bars
end

class Bar < ActiveRecord::Base
  belongs_to :foo
end

And when I'm calling

Foo.all(:include => :bars)

I see the following queries in console:

 SELECT * FROM "foos"
 SELECT "bars".* FROM "bars" WHERE ("bars".foo_id IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21))

with all the foo's ids in the where clause.

I guess this is not an optimal query while the number of ids may be large, and I need to preload all the 'bars'. Also, actually I've got not two models, but a chain of them.

Is there a way to make the eager loading query be like

SELECT "bars".* FROM "bars" 

when I'm using find all?

도움이 되었습니까?

해결책

That's actually an optimization, in fact Rails changes the querying strategy if the number of id's goes high.

You could also use :join instead of using :include

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top