On 25 Sep 2008, at 12:51, will.bryant wrote:
>
> The second bug (lighthouse #1110, testcase but no fix) seems much
> messier to fix.  I was having weird problems with records showing up
> twice in some of the application's associations when loaded.  It
> turned out that you can actually reproduce the problem relatively
> simply, if you have say a has_many which is defined with
> some :includes on it (Client has_many :projects, :include => :goals),
> and you later do a find that does the same include
> (Client.find(:all, :include => {:project => {:goals => :tasks}}) or
> even just :include => {:projects => :goals} the same as in the
> association itself), then the associations get loaded twice - and you
> get two of every object in the target array.
>
> Suddenly, all our hours and total invoice prices double... :)
>
> I need some help fixing this one.  Merging duplicate :includes in
> AssociationPreload's preload_associations works for the above example
> (and the test case on the ticket) but not the general problem, so it
> looks to me like checking to see if we've already loaded the
> association as we go is probably a better approach.  Doing it that way
> for say has_many is pretty easy, you could even just:
>

Oops. I'd noticed that in a slightly different context (i've been  
fiddling with stuff you can eager load extra associations after the  
main load). Didn't occur to me that it could happen in this way though.

> --- a/activerecord/lib/active_record/association_preload.rb
> +++ b/activerecord/lib/active_record/association_preload.rb
> @@ -129,6 +129,7 @@ module ActiveRecord
>       end
>
>       def preload_has_many_association(records, reflection,
> preload_options={})
> +        return if records.first.send(reflection.name).loaded?
>         id_to_record_map, ids = construct_id_map(records)
>         records.each {|record| record.send(reflection.name).loaded}
>         options = reflection.options
>
> But fixing it for has_many :throughs and has_ones is harder.
>
I'm not sure why that is ? (well with has_one you test for the  
existance of the appropriate ivar rather than calling loaded? but  
surely it's basically the same ?

Fred

> Thoughts?
>
> Cheers,
> Will
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to