# Eager loading, like column names, and conditions

**URL:** <https://www.ruby-forum.com/t/eager-loading-like-column-names-and-conditions/147676>\
**Category:** Rails\
**Created:** [September 4, 2008, 7:01am UTC](https://www.ruby-forum.com/t/eager-loading-like-column-names-and-conditions/147676 "2008-09-04T07:01:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [September 4, 2008, 7:01am UTC](https://www.ruby-forum.com/t/eager-loading-like-column-names-and-conditions/147676/1 "2008-09-04T07:01:12Z")

</div>

Hi all,

The setup:  
I’ve got a Candidate model, and it has two associations like:  
has\_many :next\_tasks, :class\_name =\> ‘Task’, :foreign\_key =\>  
‘candidate\_id’, :conditions =\> [‘start\_time \> now() and not deleted =  
0’], :order =\> ‘start\_time’  
has\_many :previous\_tasks, :class\_name =\> ‘Task’, :foreign\_key =\>  
‘candidate\_id’, :conditions =\> [‘start\_time \< now() and not deleted =  
0’], :order =\> ‘start\_time desc’

I have a controller which is using will\_paginate, which at some point is  
using ActiveRecord to get a count, and it’s eager loading both of these  
associations:  
:include =\> [:status, :position, :user, { :previous\_tasks =\> :user },  
{:next\_tasks =\> :user}, :hiring\_manager]

The problem:

I’m getting an error like:  
Column ‘start\_time’ in on clause is ambiguous …  
The two relevant areas of the clause are:  
LEFT OUTER JOIN `tasks` ON tasks.candidate\_id = candidates.id AND  
start\_time \< now() and not deleted = 0  
and  
LEFT OUTER JOIN `tasks` next\_tasks\_candidates ON  
next\_tasks\_candidates.candidate\_id = candidates.id AND start\_time \>  
now() and not deleted = 0

So, AR is smart enough to join ‘tasks’ as next\_tasks\_candidates, but not  
to append that join name to the conditions.

This seems (notice, just seems… I don’t pretend to be a Rails expert,  
demanding that this is a problem with Rails and not my code) to be a  
problem with AR and doing a count on a query that is eager loading two  
associations which have conditions with the same variable names in them.

Anyone have any thoughts on what I can do to fix this?

Thanks,  
Luke

---

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [September 4, 2008, 9:47am UTC](https://www.ruby-forum.com/t/eager-loading-like-column-names-and-conditions/147676/2 "2008-09-04T09:47:31Z")

</div>

On 4 Sep 2008, at 07:01, Luke I. wrote:

> 0’], :order =\> ‘start\_time desc’
> 
> Anyone have any thoughts on what I can do to fix this?

AR is not smart enough to parse your sql fragments and qualify table  
names. You’d probably have to basically parse the sql exactly as the  
database does to do that.

What you can do is have an interpolated condition. Off the top of my  
head,

:conditions =\> ‘…#{aliased\_table\_name}.start\_time…’, …

The single quotes are important.

Fred

---

<div class="post-metadata">

**Author:** ![technodolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@technodolt](https://www.ruby-forum.com/u/technodolt)\
**Post date:** [September 4, 2008, 10:04am UTC](https://www.ruby-forum.com/t/eager-loading-like-column-names-and-conditions/147676/3 "2008-09-04T10:04:44Z")

</div>

Frederick C. wrote:

> On 4 Sep 2008, at 07:01, Luke I. wrote:
> 
> > 0’], :order =\> ‘start\_time desc’
> > 
> > Anyone have any thoughts on what I can do to fix this?
> 
> AR is not smart enough to parse your sql fragments and qualify table  
> names. You’d probably have to basically parse the sql exactly as the  
> database does to do that.
> 
> What you can do is have an interpolated condition. Off the top of my  
> head,
> 
> :conditions =\> ‘…#{aliased\_table\_name}.start\_time…’, …
> 
> The single quotes are important.
> 
> Fred

Actually:

I JUST found this out right before I read your reply:

If you, in your conditions, specify the table name, so in my case  
something like:

has\_many :next\_tasks, :class\_name =\> ‘Task’, :foreign\_key =\>  
‘candidate\_id’, :conditions =\> [‘tasks.start\_time \> now() and not  
tasks.deleted = 0’], :order =\> ‘start\_time’

When it does the join, it WILL automatically replace the tasks. part  
with the correct entry from the SQL query that it generates.

Thanks for the help, though, it’s good to know that the alternative  
option exists as well.

🙂

-Luke
