Bugs in eager loading of has_one :through and has_many :through

62 views
Skip to first unread message

Joseph Palermo

unread,
Jan 9, 2009, 3:46:11 PM1/9/09
to Ruby on Rails: Core
Failing test can be found here:
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1723-eager-load-of-has_one-through-with-conditions-on-the-through-table-fails

Eager loading of has_one :through has an easy to fix problem. It
doesn't pass the :conditions or :order of the base association down to
the "source" include on the :through association. has_many :through
does this already, the "options" just need to be passed down.

The larger problem, of both has_one :through and has_many :through is
when you have a :conditions that references the :through table. Since
preload never does any joining, it always does the associations
independently, it throws a SQL error because it can't find that table.

This is only a problem with eager loading, the find_every code in
base.rb does the correct detection normally.

Frederick Cheung

unread,
Jan 11, 2009, 5:41:30 AM1/11/09
to rubyonra...@googlegroups.com

On 9 Jan 2009, at 20:46, Joseph Palermo wrote:

>
> Failing test can be found here:
> http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1723-eager-load-of-has_one-through-with-conditions-on-the-through-table-fails
>
> Eager loading of has_one :through has an easy to fix problem. It
> doesn't pass the :conditions or :order of the base association down to
> the "source" include on the :through association. has_many :through
> does this already, the "options" just need to be passed down.
>
> The larger problem, of both has_one :through and has_many :through is
> when you have a :conditions that references the :through table. Since
> preload never does any joining, it always does the associations
> independently, it throws a SQL error because it can't find that table.
>
I've been vaguely meaning to rewrite the preload code so that it loads
hmt associations in one go (rather than loading the records in two
steps).
There's a preliminary task which is to refactor the hmt code to make
it easy for the preload stuff to recreate the appropriate joins (don't
really fancy duplicating all that like in join based eager loading).

Fred
Reply all
Reply to author
Forward
0 new messages