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:inload_missing_constant'const_missing’
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/dependencies.rb:80:in
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.
@ 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
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