rails/rails — Issue #3281: Uninitialized Constant for STI Polymorphic Associations with :source
Author: rizwanreza
Created: 2011-10-10 14:52:43+00:00
State: open

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!