This project is archived and is in readonly mode.

#409 ✓stale
Olafski

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

Reported by Olafski | June 13th, 2008 @ 04:13 PM

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

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages