This project is archived and is in readonly mode.

#5429 ✓invalid
Ivan Konyshev

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

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

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
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

Comments and changes to this ticket

  • Ivan Konyshev

    Ivan Konyshev August 23rd, 2010 @ 01:16 AM

    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.

  • Ivan Konyshev

    Ivan Konyshev August 23rd, 2010 @ 01:18 AM

    • Title changed from “Error in ActiveRecord::Base compute_type method” to “It's hard to understand errors generated by ActiveRecord::Base compute_type method”
  • Andrew White

    Andrew White August 23rd, 2010 @ 12:35 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

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

    class Category
      def 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).

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=""></a>