Ruby on Rails 3:“类的超类不匹配......”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5512023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails 3 : "superclass mismatch for class ..."
提问by Matthew
Platform: Mac OSX 10.6
平台:Mac OSX 10.6
In my terminal, i start the Ruby console with "rails c"
在我的终端中,我使用“rails c”启动 Ruby 控制台
While following the Ruby on Rails 3 tutorial to build a class:
按照 Ruby on Rails 3 教程构建一个类:
class Word < String
def palindrome? #check if a string is a palindrome
self == self.reverse
end
end
i get the 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 problem and i know the code is fine; I've searched other related questions, but they all involved migrating from Ruby 2 to 3 or erb vs eruby.
教程显示它没有问题,我知道代码很好;我搜索了其他相关问题,但它们都涉及从 Ruby 2 迁移到 3 或 erb vs eruby。
回答by Douglas F Shearer
You already have a Wordclass defined elsewhere. I tried within a Rails 3 app but was not able to replicate.
您已经在Word别处定义了一个类。我在 Rails 3 应用程序中尝试过,但无法复制。
If you have not created a second Wordclass yourself, it is likely one of your Gems or plugins already defines it.
如果您还没有Word自己创建第二个类,则您的 Gems 或插件之一可能已经定义了它。
回答by Kris
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
The require loads Document(which has a superclass of Object) before Document < ActiveRecord::Base(which has a different superclass).
require 加载Document(它有一个 Object 的超类)之前Document < ActiveRecord::Base(它有一个不同的超类)。
I should note that in a Rails environment the require is not usually needed since it has auto class loading.
我应该注意到,在 Rails 环境中,通常不需要 require,因为它具有自动类加载。
回答by 2called-chaos
I had the problem with a Rails 4 application. I used concerns under the user namespace.
我遇到了 Rails 4 应用程序的问题。我在用户命名空间下使用了关注点。
class User
module SomeConcern
end
end
In development everything worked fine but in production (I guess because of preload_app true) I got the mismatch error. The fix was pretty simple. I just added an initializer:
在开发中一切正常,但在生产中(我猜是因为 preload_app true)我得到了不匹配错误。修复非常简单。我刚刚添加了一个初始化程序:
require "user"
Cheers!
干杯!
回答by lulalala
Sometimes we 'open class' without us knowing. For example with some deep module nesting:
有时我们在不知情的情况下“公开课”。例如一些深层模块嵌套:
# 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 trigger, we open the existing SpaceGun class. This works. However if we load the two file in the reverse order, the error would be raised, because we would define a SpaceGun class first, but is not a Weapon.
当我们定义触发器时,我们打开现有的 SpaceGun 类。这有效。但是,如果我们以相反的顺序加载这两个文件,则会引发错误,因为我们将首先定义 SpaceGun 类,但它不是武器。
Sometimes we make this mistake because we explicitly require sub module (e.g. trigger) from the parent class. Which means the class definition will be done the reverse order, causing this issue.
有时我们会犯这个错误,因为我们显式地从父类中要求子模块(例如触发器)。这意味着类定义将以相反的顺序完成,从而导致此问题。
# 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
Either
任何一个
- rely on auto loading
- always put inheritance to every open class occurrence.
- 依赖自动加载
- 始终将继承放在每个开放类出现中。
回答by RebelWarrior
I had this same problem right now. Basically that means that Word is defined as a class elsewhere and my guess is that it's on the rail-ties gem. Just change Word to Word2 and it should work fine on the tutorial.
我现在遇到了同样的问题。基本上这意味着 Word 在其他地方被定义为一个类,我的猜测是它在轨道上。只需将 Word 更改为 Word2,它就可以在教程中正常工作。

