rails/rails — Issue #3247: After upgrading from 3.1 to 3.1.1 has_many ... :source changed its behaviour
Author: stanislaw
Created: 2011-10-08 01:08:34+00:00
State: open

```ruby
class Cmodule < ActiveRecord::Base
has_many :items_cmodules
has_many :cycles, :order => :position, :through => :items_cmodules, :source_type => "Cycle", :source => :item
has_many :techniques, :order => :position, :through => :items_cmodules, :source_type => "Technique", :source => :item
end

class ItemsCmodule < ActiveRecord::Base
belongs_to :cmodule
belongs_to :item, :polymorphic => true
end

class Technique < ActiveRecord::Base
has_many :items_cmodules, :as => :item
end
```

Before upgrading to 3.1.1 following worked fine:

```ruby
Cmodule.first.techniques.create(:name => "blip")
```

Right after upgrading it gives following output:

```ruby
ruby-1.9.2-p180 :003 > Cmodule.first.techniques.create(:name => "blip")
Cmodule Load (0.0ms) SELECT "cmodules".* FROM "cmodules" LIMIT 1
(0.0ms) BEGIN
SQL (0.0ms) INSERT INTO "techniques" ("annotation", "created_at", "icon_content_type",
"icon_file_name", "icon_file_size", "icon_updated_at", "instruction", "name", "status",
"updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"
[["annotation", nil], ["created_at", Sat, 08 Oct 2011 03:59:37 FET +03:00],
["icon_content_type", nil], ["icon_file_name", nil], ["icon_file_size", nil],
["icon_updated_at", nil], ["instruction", nil], ["name", "blip"], ["status", "draft"],
["updated_at", Sat, 08 Oct 2011 03:59:37 FET +03:00]]
(0.0ms) ROLLBACK
NameError: uninitialized constant ItemsCmodule::Item
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/base.rb:1341:in
`compute_type'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/reflection.rb:173:in
`klass'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/reflection.rb:228:in
`association_primary_key'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/through_association.rb:47:in
`block in construct_join_attributes'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/through_association.rb:46:in
`map'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/through_association.rb:46:in
`construct_join_attributes'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/has_many_through_association.rb:54:in
`through_record'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/has_many_through_association.rb:45:in
`insert_record'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:425:in
`block (2 levels) in create_record'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:337:in
`add_to_target'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:423:in
`block in create_record'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:147:in
`block in transaction'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in
`transaction'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/transactions.rb:208:in
`transaction'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:146:in
`transaction'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:422:in
`create_record'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:111:in
`create'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/activerecord-3.1.1/lib/active_record/associations/collection_proxy.rb:54:in
`create'
from (irb):3
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in
`start'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in
`start'
from /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/railties-3.1.1/lib/rails/commands.rb:40:in
`'
from script/rails:6:in `require'
from script/rails:6:in `

'

```

Sorry for not providing more qualitative material. I don't know, how to spot it right now.