Forum: Ruby on Rails Rails 2.2 - Eager loading not working?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1981f87f5be54f66e535d348ab07247b?d=identicon&s=25 Adriano (Guest)
on 2008-12-23 19:00
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-23 19:01
(Received via mailing list)
On Dec 23, 5:27 pm, Adriano <adri...@peabirus.com.br> wrote:

> Am I missing something?
>
That's how it works in rails 2.1 and above

Fred
1981f87f5be54f66e535d348ab07247b?d=identicon&s=25 Adriano (Guest)
on 2008-12-23 19:11
(Received via mailing list)
On Dec 23, 4:00 pm, Frederick Cheung <frederick.che...@gmail.com>
wrote:
> On Dec 23, 5:27 pm, Adriano <adri...@peabirus.com.br> 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-23 19:22
(Received via mailing list)
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-12-23 19:49
(Received via mailing list)
On Dec 23, 2008, at 1:22 PM, Frederick Cheung 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.