Ruby on Rails 3: "superclass mismatch for class ..."

Platform: Mac OSX 10.6

In my terminal, I launch the Ruby console using "rails c"

At runtime tutorial Ruby on Rails 3 to create a class:

class Word < String def palindrome? #check if a string is a palindrome self == self.reverse end end 

I get an error message:

 TypeError: superclass mismatch for class Word from (irb):33 from /Users/matthew/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start' from /Users/matthew/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start' from /Users/matthew/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>' 

The tutorial shows that it has no problems, and I know that the code is fine; I was looking for other related questions, but all of them included switching from Ruby 2 to 3 or erb vs eruby.

+56
ruby ruby-on-rails ruby-on-rails-3
Apr 01 '11 at 10:20
source share
5 answers

You already have a Word class defined elsewhere. I tried in Rails 3 but could not replicate.

If you did not create the second Word class yourself, probably one of your Gems or plugins already defines it.

+71
Apr 01 2018-11-11T00:
source share

This can also happen as such:

 # /models/document/geocoder.rb class Document module Geocoder end end # /models/document.rb require 'document/geocoder' class Document < ActiveRecord::Base include Geocoder end 

It requires loading a Document (which has a superclass of the object) to Document < ActiveRecord::Base (which has a different superclass).

I should note that in a Rails environment, a requirement is usually not required, since it has autoclave loading.

+24
Mar 14 2018-12-12T00:
source share

I had a problem with the Rails 4 application. I used problems in the user namespace.

 class User module SomeConcern end end 

Everything worked fine in development, but in production (I think due to preload_app true) I got a mismatch error. The fix was pretty simple. I just added an initializer:

 require "user" 

Hurrah!

+17
Mar 11 '14 at 6:28
source share

I had the same problem right now. This basically means that Word is defined as a class elsewhere, and I assume that it is at the pearl of railway connections. Just change Word to Word2 and it should work well in the tutorial.

+6
Oct 30 2018-11-11T00:
source share

Sometimes we open a class without knowing us. For example, with a certain depth of nesting of modules:

 # space_gun.rb class SpaceGun << Weapon def fire Trigger.fire end end # space_gun/trigger.rb class SpaceGun class Trigger end end 

When we define a trigger, we open the existing SpaceGun class. It works. However, if we upload two files in the reverse order, the error will be raised because we first define the SpaceGun class, but are not weapons.

Sometimes we make this mistake because we explicitly require that a module (for example, a trigger) be subordinate from a module of the parent class. This means that the class definition will be done in the reverse order, which will cause this problem.

 # surely nothing can go wrong if we require what we need first right? require 'space_gun/trigger' class SpaceGun << Weapon def fire Trigger.fire end end # BOOM 

Or

  • rely on automatic downloads
  • always puts inheritance in every open class event.
+3
Jul 13 '16 at 5:32
source share



All Articles