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”?