Ambiguous column name in has_many :through

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:inexecute’
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:inselect_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:infind_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:infind’
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”?

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.
[email protected]
itsdEx.com

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