#409 new
Olafski

has_many :through with :source does not work correctly with find(:include)

Reported by Olafski | June 13th, 2008 @ 10:13 AM

Sorry I did not provide a proper attachment with tests, etc. But I have no idea how to do that :)

Given the following code:

Cargo model:

class Cargo < ActiveRecord::Base
  belongs_to :load_stop, 
             :foreign_key => 'load_stop_id', 
             :class_name => 'Stop'
end

Stop model:

class Stop < ActiveRecord::Base
  has_many :cargo_loads, 
           :class_name => 'Cargo',
           :foreign_key => 'load_stop_id'


  belongs_to :transports
end

Transport model

class Transport < ActiveRecord::Base
  has_many :stops
  
  has_many :cargos, 
           :through => :stops, 
           :source => :cargo_loads
end

Output of the console:

Basically,

Transport.find(:first).cargos

works, but

Transport.find(:first, :include => :cargos)

gives an error telling me that the column cargos.stop_id does not exist.

>> Transport.find(:first).cargos
=> [#<Cargo id: 1, load_stop_id: 1, created_at: "2008-06-13 16:40:39", updated_at: "2008-06-13 16:42:12">]
>> Transport.find(:first, :include => :cargos)
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: cargos.stop_id: SELECT transports."id" AS t0_r0, transports."created_at" AS t0_r1, transports."updated_at" AS t0_r2, cargos."id" AS t1_r0, cargos."load_stop_id" AS t1_r1, cargos."created_at" AS t1_r2, cargos."updated_at" AS t1_r3 FROM transports  LEFT OUTER JOIN stops ON (transports."id" = stops."transport_id")  LEFT OUTER JOIN cargos ON (cargos."stop_id" = stops."id")   
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:150:in `log'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/sqlite_adapter.rb:132:in `execute'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/sqlite_adapter.rb:345:in `catch_schema_changes'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/sqlite_adapter.rb:132:in `execute'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/sqlite_adapter.rb:256:in `select'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:55:in `select_all'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1242:in `select_all_rows'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1124:in `find_with_associations'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1122:in `catch'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations.rb:1122:in `find_with_associations'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1232:in `find_every'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1227:in `find_initial'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:502:in `find'
        from (irb):2
>>

Comments and changes to this ticket

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

The latest development for the 1.2.x and 2.0.x releases are on the 1-2-stable and 2-0-stable branches.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins