Repository: rails/rails

Issue #3247: After upgrading from 3.1 to 3.1.1 has_many ... :source changed its behaviour
Created: 2011-10-08T01:08:34Z
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:
Cmodule.first.techniques.create(:name => "blip")
Right after upgrading it gives following output:
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 
`<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
Sorry for not providing more qualitative material. I don't know, how to spot it right now.

Duplicate issue #3281: Uninitialized Constant for STI Polymorphic Associations with :source
Created: 2011-10-10T14:52:43Z
Here's the relevant code:
class Industry < ActiveRecord::Base
  has_many :companies,
    :through => :industry_assignments,
    :source => :industrializable,
    :source_type => "Company"
end

class IndustryAssignment < ActiveRecord::Base
  belongs_to :industrializable, :polymorphic => true
  belongs_to :industry
end

When I create a new record, for example, with the following:
i = Industry.new
i.name = 'Electronics'
i.save!
i.companies = [Company.first]

I get the following error:
NameError: uninitialized constant IndustryAssignment::Industrializable
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/base.rb:1341:in `compute_type'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/reflection.rb:173:in `send'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/reflection.rb:173:in `klass'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/reflection.rb:228:in `association_primary_key'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/through_association.rb:47:in `construct_join_attributes'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/through_association.rb:46:in `map'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/through_association.rb:46:in `construct_join_attributes'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/has_many_through_association.rb:54:in `through_record'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/has_many_through_association.rb:45:in `insert_record'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:128:in `concat'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:337:in `add_to_target'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:127:in `concat'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:125:in `each'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:125:in `concat'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:147:in `transaction'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/transactions.rb:208:in `transaction'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:146:in `transaction'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:124:in `concat'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/has_many_through_association.rb:31:in `concat'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:305:in `replace'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:147:in `transaction'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/transactions.rb:208:in `transaction'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:146:in `transaction'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:302:in `replace'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/collection_association.rb:41:in `writer'
    from /Users/rizwanreza/.rvm/gems/ree-1.8.7-2011.03@monaqasat3/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb:49:in `companies='

This was working in Rails 3.0.10. Any ideas?
Thanks!

Closing comment (2011-10-10T17:12:32Z): This looks like a dupe of #3247, so I'll close this ticket. I'll definitely fix it for 3.1.2. For now you can work around it either by downgrading to 3.1.0, or but specifying :primary_key => :id on your :industrializable association.