Rails 2.2 - Eager loading not working?

Hi,

I'm using Rails 2.2 with SQlite. Eager loading doesn't seem to be working, instead of a join two selects are executed:

class Category < ActiveRecord::Base   has_many :tools end

class Tool < ActiveRecord::Base   belongs_to :category end

Tool.find(:all, :include => :category)

Tool Load (53.1ms) SELECT * FROM "tools" Category Load (6.0ms) SELECT * FROM "categories" WHERE ("categories"."id" = 1)

Category.find(:all, :include => :tools)

Category Load (1.9ms) SELECT * FROM "categories" Tool Load (8.8ms) SELECT "tools".* FROM "tools" WHERE ("tools".category_id IN (1,2,3,4,5,6,7,8,9))

Am I missing something?

Thanks,

Am I missing something?

That's how it works in rails 2.1 and above

Fred

Ah, thanks.

I guess I didn't read the release notes carefully (I'm curious to know the rationale behind the change).

Am I missing something?

That's how it works in rails 2.1 and above

Ah, thanks.

I guess I didn't read the release notes carefully (I'm curious to know the rationale behind the change).

Generally, small simpler querys often end up being easier to handle.
join based include was also slow if you included two has_many
associations at the same time (because the result set would
effectively have the product of those two associations). Code is quite
a bit simpler too.

Fred

Because when you get up to an eager load of something like:

     find(:all, :include => { :address => [:state, :country],             :school => { :admin_contact => { :address => [:state, :country]}}},           :conditions => { :type => 'ReceivingAccount', :aasm_state => 'in_production' })

(yes, this is real) The resulting set of queries is much more efficient that the huge single-query equivalent.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com