Will ReadWill Read
Standup 8/21/2009: "Not Missing Constant" and "Rake Set Theory"
edit Posted by Will Read on Friday August 21, 2009 at 09:16AM

Interesting Things

  • "... not missing constant" ERROR To reproduce this error you do something like this:

Generate a rails app, call it "test".

Create a module scoped model called Post in app/models/mumble/post.rb

class Mumble::Comment < ActiveRecord::Base
  belongs_to :post
end

Create a module scoped model called Comment in app/models/mumble/comment.rb

class Mumble::Comment < ActiveRecord::Base
  belongs_to :post
end

Then at the command line

$ script/console
Loading development environment (Rails 2.3.3)

comment = Mumble::Comment.first
=> #
comment.post
=> #
exit
$ script/console
Loading development environment (Rails 2.3.3)
post = Mumble::Post.new
=> #
exit
$ script/console
Loading development environment (Rails 2.3.3)
post = Mumble::Post.create!
=> #
comment = post.comments.create!
=> #
comment.post
ArgumentError: Mumble is not missing constant Post!
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/dependencies.rb:417:in load_missing_constant' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/dependencies.rb:80:inconst_missing'

Apparently, there are various situations that can cause the infamous "Mumble is not missing constant Post!" error. In this case it appears the associations do not understand the module scoping, despite the statement:

By default, associations will look for objects within the current module scope.

at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

The solution was to explicitly declare the class name in the association:

class Mumble::Post < ActiveRecord::Base
  has_many :comments, :class_name => "Mumble::Comment"
end

and

class Mumble::Comment < ActiveRecord::Base
  belongs_to :post, :class_name => "Mumble::Post"
end

Then to show the error is no longer present

$ script/console
Loading development environment (Rails 2.3.3)

post = Mumble::Post.create!
=> #
comment = post.comments.create!
=> #
comment.post
=> #

  • "Rake Set Theory" when running the same thing in rake multiple times, Rake strips out the extra commands. For example: $rake db:rollback db:rollback which you might expect to preform two rollbacks, only does one rollback. This can be frustrating if you're trying to couple rollbacks with a db:test:prepare or some other logical chain of events. At the command line you can of course work around most situations by && together multiple rake commands.

Comments

  1. grosser grosser on August 21, 2009 at 12:22PM

    @ Rake Set Theory This hit me too when doing something like

    task :x do
       something.each{ Rake::Task[:y].invoke }
    end
    

    :y would only run once, which was kind of unexpected...

  2. Andrew White Andrew White on August 22, 2009 at 09:13AM

    I've had a patch for the "not missing constant" message in the Lighthouse tracker for a while: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2283

    The issue is not that AR associations don't understand module scope it's that the lookup is done via const_missing which raises (IMO unnecessary) the exception if the constant already exists, which it will do if it already has been loaded via some other route.