Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

It's hard to understand errors generated by ActiveRecord::Base compute_type method

#5429

Strange exception occurs when try to build association in helper

f.object.send(association).build , where f.object type is Category and association is :rating_criterias

got error:
uninitialized constant Category::RatingCriteria
trace:
activerecord (3.0.0.rc) lib/active_record/base.rb:1139:in compute_type'<br/> activerecord (3.0.0.rc) lib/active_record/reflection.rb:169:insend'
activerecord (3.0.0.rc) lib/active_record/reflection.rb:169:in klass'<br/> activerecord (3.0.0.rc) lib/active_record/reflection.rb:205:inquoted_table_name'
activerecord (3.0.0.rc) lib/active_record/associations/has_many_association.rb:102:in construct_sql'<br/> activerecord (3.0.0.rc) lib/active_record/associations/association_collection.rb:24:ininitialize'
activerecord (3.0.0.rc) lib/active_record/associations/has_many_association.rb:11:in initialize'<br/> activerecord (3.0.0.rc) lib/active_record/associations.rb:1405:innew'
activerecord (3.0.0.rc) lib/active_record/associations.rb:1405:in rating_criterias'<br/> app/helpers/application_helper.rb:7:inlink_to_add_fields'

After some investigation I found this:
activerecord-3.0.0.rc/lib/active_record/base.rb in compute_type method:
name.scan(/::|$/) { candidates.unshift "#{$}::#{type_name}" }

if name has no "::" then "name::type_name" is added to candidates instead of type_name itself

Reported by Ivan Konyshev · August 23rd, 2010 @ 12:35 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Ivan Konyshev
    Ivan Konyshev

    After all it was mistake in my RatingCriteria definition.. Error message is completely misunderstandable.
    ArgumentError was swallowed and uninitialized constant error generated with name of first candidate.

    August 23rd, 2010 @ 01:16 AM

  2. Ivan Konyshev
    Ivan Konyshev
    • Title changed from Error in ActiveRecord::Base compute_type method to It's hard to understand errors generated by ActiveRecord::Base compute_type method

    August 23rd, 2010 @ 01:18 AM

  3. Andrew White
    Andrew White
    • State changed from new to invalid
    • Importance changed from to Low

    The error message is the same as you would get with pure ruby:

    #!/usr/bin/ruby
    
    class Category
      def build
        RatingCriteria.new
      end
    end
    
    Category.new.build
    >> uninitialized constant Category::RatingCriteria (NameError)
    

    The rescue for ArgumentError is there because constantize can raise one in certain circumstances but the correct class will eventually be found. The error message can't specifically refer to setting :class_name for an association as it's used elsewhere as well (e.g. STI).

    August 23rd, 2010 @ 12:35 PM