Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Standup 8/21/2009: "Not Missing Constant" and "Rake Set Theory"

Will Read
Friday, August 21, 2009

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:in
const_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.
  • 0 Shares
  • Share on Facebook
  • Share on Twitter

2 Comments

  1. grosser says:

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

    August 21, 2009 at 12:22 pm

  2. Andrew White says:

    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.

    August 22, 2009 at 9:13 am

Add New Comment Cancel reply

Your email address will not be published.

Will Read

Will Read
San Francisco

Recent Posts

  • Org Chart Growth and Keeping Our “Flatness”
  • Releasing When It’s Ugly
  • “Drinking the Kool-Aid™” How We Create Value Alignment
Subscribe to Will's Feed

Author Topics

agile (31)
bloggerdome (3)
process (1)
"soft" ware (16)
feedback (1)
engines (1)
documentation (1)
tdd (2)
bdd (1)
breakfast (1)
pairing (1)
testing (3)
api (2)
bug (1)
chore (1)
velocity (1)
oauth (1)
queues (3)
version control (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >