Rails Class Collisions

Posted by Curtis Miller Curtis Miller

While trying to use the oh-so-nifty missing spec finder on a recent project, I discovered that it was not working. Hmmm… seems that somewhere along the way Rails added detection for existing constants and won't allow you to re-generate something. Instead it calls raise_class_collision, which raises a UsageError. Damn them.

Anyway, to work around this for now, you can add the following somewhere (e.g., an initializer):

require 'rails_generator/base'
require 'rails_generator/commands'

module Rails
  module Generator
    module Commands
      class Create
        def raise_class_collision(class_name)
          # Do Nothing
        end
      end
    end
  end
end

This should get around that inconvenience. Plus, you won't be exposed to nefarious suggestions like I was:

$ rake spec:sync
  The name 'User' is either already used in your application or reserved by Ruby on Rails.
  Please choose an alternative and run this generator again.

  Suggestions:

exploiter
drug user
substance abuser

Thanks Rails, you're so helpful! I just don't understand how it could have known I was building a Facebook clone for junkies…

Other Resources



Velocity Labs

Need web application development, maintenance for your existing app, or a third party code review?

Velocity Labs can help.

Hire us!