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"? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060731/e641aa22/attachment.html
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 Paul gustav@rails.co.za itsdEx.com -- Posted via http://www.ruby-forum.com/.
Jeremiah Peschka
2006-Jul-31 18:29 UTC
[Rails] Re: ambiguous column name in has_many :through
That was it, it''s now working beautifully. Thanks. On 7/31/06, gustav Paul <gustav@rails.co.za> wrote:> > 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 Paul > gustav@rails.co.za > itsdEx.com > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060731/14621a36/attachment.html