# Ambiguous column name in has\_many :through

**URL:** <https://www.ruby-forum.com/t/ambiguous-column-name-in-has-many-through/100733>\
**Category:** Rails\
**Created:** [July 31, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/ambiguous-column-name-in-has-many-through/100733 "2006-07-31T19:47:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremiah\_Peschka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@Jeremiah\_Peschka](https://www.ruby-forum.com/u/Jeremiah_Peschka)\
**Post date:** [July 31, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/ambiguous-column-name-in-has-many-through/100733/1 "2006-07-31T19:47:45Z")

</div>

In my application model I have three classes: Accounts, Projects, and  
Items

class Accounts \< ActiveRecord::Base  
has\_many :items, :through =\> :projects, :source =\> :items  
has\_many :late\_items, :class\_name =\> ‘Item’, :through =\> :projects,  
:conditions =\> [‘due\_on \< ?’, Time.now], :source =\> :items  
end

class Projects \< ActiveRecord::Base  
has\_many :items  
belongs\_to :account  
end

class Item \< ActiveRecord::Base  
belongs\_to :project  
end

When I attempt to grab an account’s items through @my\_account.items  
everything is fine. However, when I try to find the late items with  
@my\_account.late\_items I get a RuntimeError:

ActiveRecord::StatementInvalid: RuntimeError: ERROR C42702 Mcolumn  
reference “closed\_at” is ambiguous Fparse\_relation.c L360  
RscanRTEForColumn: SELECT items.\* FROM items INNER JOIN projects ON  
items.project\_id = projects.id WHERE (projects.account\_id = NULL AND  
(closed\_at IS NULL AND due\_on \< ‘2006-07-31 13:40:17’))  
from  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/connection\_adapters/abstract\_adapter.rb:120:in  
`log' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/postgresql_adapter.rb:148:in`execute’  
from  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/connection\_adapters/postgresql\_adapter.rb:361:in  
`select' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/postgresql_adapter.rb:129:in`select\_all’  
from  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/base.rb:390:in  
`find_by_sql' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:924:in`find\_every’  
from  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/base.rb:381:in  
`find' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/associations/has_many_through_association.rb:36:in`find’  
from ./script/…/config/…/config/…/app/models/account.rb:65:in  
`late\_items’  
from (irb):7

Is there anything I can change in my model to avoid this error and still  
find the late\_items?

Should I just modify the conditions of my join to include  
“items.closed\_atIS NULL AND  
projects.closed\_at IS NULL”?

---

<div class="post-metadata">

**Author:** ![Jeremiah\_Peschka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@Jeremiah\_Peschka](https://www.ruby-forum.com/u/Jeremiah_Peschka)\
**Post date:** [July 31, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/ambiguous-column-name-in-has-many-through/100733/2 "2006-07-31T20:14:27Z")

</div>

Jeremiah Peschka wrote:

> In my application model I have three classes: Accounts, Projects, and  
> Items
> 
> class Accounts \< ActiveRecord::Base  
> has\_many :items, :through =\> :projects, :source =\> :items  
> has\_many :late\_items, :class\_name =\> ‘Item’, :through =\> :projects,  
> :conditions =\> [‘due\_on \< ?’, Time.now], :source =\> :items  
> end
> 
> class Projects \< ActiveRecord::Base  
> has\_many :items  
> belongs\_to :account  
> end
> 
> class Item \< ActiveRecord::Base  
> belongs\_to :project  
> end

> Is there anything I can change in my model to avoid this error and still  
> find the late\_items?
> 
> Should I just modify the conditions of my join to include  
> “items.closed\_atIS NULL AND  
> projects.closed\_at IS NULL”?

In my experience this usually means that more than one of the tables in  
question have the due\_on column…try and explicitly refer to it thusly:

(assuming you want projects due\_on yesterday, if its items, use  
items.due\_on instead)

:conditions =\> [‘projects.due\_on \< ?’, Time.now], :source =\> :items

Hope it helps!  
Ciao!  
Gustav P.  
removed\_email\_address@domain.invalid  
[itsdEx.com](http://itsdEx.com)

---

<div class="post-metadata">

**Author:** ![Jeremiah\_Peschka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@Jeremiah\_Peschka](https://www.ruby-forum.com/u/Jeremiah_Peschka)\
**Post date:** [July 31, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/ambiguous-column-name-in-has-many-through/100733/3 "2006-07-31T20:29:51Z")

</div>

That was it, it’s now working beautifully. Thanks.
