Calling respond_to? on something that might be a scoped association can cause the full collection to load
Reported by Tom Locke | September 9th, 2008 @ 01:05 PM | in Hobo 1.0 - Final
Comments and changes to this ticket
-
Tom Locke September 9th, 2008 @ 01:05 PM
- State changed from new to open
-
Tom Locke March 12th, 2009 @ 11:36 AM
- Milestone changed from Hobo 1.0 - Final to Beyond Hobo 1.0
-
Betelgeuse December 9th, 2009 @ 02:21 PM
Please make this part of Hobo 1.0 - RC 1 because this makes using <count:> in index pages have horrible
performance:>> o.childs.loaded? => false >> o.childs.try.to_int => nil >> o.childs.loaded? => true >> HoboSupport::VERSION => "0.9.102"
Because count has the following code it means using it causes a full load:
c = this.try.to_int || this.try.total_entries || (this.try.loaded? && this.try.length) || th is.try.count || this.try.length
-
Betelgeuse December 9th, 2009 @ 07:41 PM
Actually it doesn't seem to be respond_to? but method_missing that's causing the target to load as the association is trying to relay calls to the target:
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:212:in
method_missing' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:371:inmethod_missing_without_paginate' /Library/Ruby/Gems/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:inmethod_missing' /Library/Ruby/Gems/1.8/gems/hobo-0.9.0/taglibs/rapid_core.dryml:515:in
count' -
Tom Locke December 10th, 2009 @ 04:16 PM
I agree this is quite important, especially index pages have the count tag on them by default. But perhaps we could fix this in
<count>
in time for Hobo 1.0, and come back to the tougher problem? -
Betelgeuse December 10th, 2009 @ 04:38 PM
It's one possibility but just fixing count could leave other pitfalls like this around. Here's the quick fix I used for count in my application in case someone else needs it:
has_many :childs, :dependent => :destroy do def to_int; count; end def is_a?(type); false; end end
If you need test code, here's what I used
it "should not load all childs when rendering index" do get :index response.should be_success list = assigns[:this] list.size.should > 0 list.each do |o| o.childs.loaded?.should be_false end end
-
Betelgeuse December 12th, 2009 @ 03:18 PM
I opened #584 to make sure at least count gets fixed before 1.0.
-
Matt Jones December 13th, 2009 @ 12:23 AM
Some further notes on this issue:
-
the direct loading from calling respond_to? is a side-effect of the implementation AssociationCollection inherits from AssociationProxy; it's relatively straightforward to fix this and avoid loading records. For now, I'll put it in the file that's already tweaking bits of AssociationCollection.
-
on scopes, however, the 'try' method is getting delegated to proxy_found (in named_scope.rb), so that can cause association loads as well. Note that only scope proxies do this; the plumbing is different in the AssociationCollection case, for no particularly apparent reason.
Not sure what the best fix for the latter is, as it's caused by the name clash between HoboSupport's try and Rails 2.3's try. Maybe a rename is in order?
-
-
Matt Jones December 13th, 2009 @ 12:38 AM
(from [a289c139779b76bb31a9ef8db94ee839270cb179]) avoid unwanted DB loads on respond_to? and try. [#261] http://github.com/tablatom/hobo/commit/a289c139779b76bb31a9ef8db94e...
-
Matt Jones December 13th, 2009 @ 12:41 AM
@Betelgeuse: can you try out the above commit and see if it's resolved the issue? It worked in my limited test setup, but there may be more places that are causing this problem.
To correct my previous comment @ 12:23AM, I dealt with the try issue the same way someone already had in methodcall.rb (for AssociationCollection) - adding the method back explicitly.
-
Bryan Larsen December 14th, 2009 @ 10:38 PM
- Milestone changed from Beyond Hobo 1.0 to Hobo 1.0 - Final
just marking 1.0 so we don't lose track of it.
-
Tom Locke December 16th, 2009 @ 10:17 PM
Bryan - are you disagreeing with Betelgeuse that this is resolved now?
-
Bryan Larsen December 16th, 2009 @ 10:32 PM
Nope, I'm just saying I haven't looked closely enough to close it myself, and was hoping Matt would do so.
-
Matt Jones December 16th, 2009 @ 11:12 PM
- Tag cleared.
- State changed from open to resolved
#584 captures the other half of this (with is_a?), so I'll close it.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
People watching this ticket
Referenced by
- 247 Unused DB fetch in Agility I've created #261 to track this
- 584 Using count in index page cards leads to loading the whole collection Splitting from #261 to ensure at least this gets resolved...
- 261 Calling respond_to? on something that might be a scoped association can cause the full collection to load (from [a289c139779b76bb31a9ef8db94ee839270cb179]) avoid u...