Rails 2.2 - Eager loading not working?


#1

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,


Adriano


#2

On Dec 23, 5:27 pm, Adriano removed_email_address@domain.invalid wrote:

Am I missing something?

That’s how it works in rails 2.1 and above

Fred


#3

On Dec 23, 4:00 pm, Frederick C. removed_email_address@domain.invalid
wrote:

On Dec 23, 5:27 pm, Adriano removed_email_address@domain.invalid wrote:

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).


Adriano


#4

On 23 Dec 2008, at 18:09, Adriano wrote:

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


#5

On Dec 23, 2008, at 1:22 PM, Frederick C. wrote:

Fred


Adriano

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 B. http://agileconsultingllc.com
removed_email_address@domain.invalid