Rails找不到与Ruby类同名的模型

时间:2023-01-02 20:24:14

I'm fairly new to Ruby on Rails, and I have a project with a "Set" model. This is in Rails 2.3.2. Now the problem is that it can't find any methods on that model's class at all. For example: "undefined method find' for Set:Class" or "undefined methoderrors' for #". It seems to be trying to find those methods on the Ruby "Set" class instead my model class.

我是Ruby on Rails的新手,我有一个带有“Set”模型的项目。这是在Rails 2.3.2中。现在问题是它根本找不到该模型类的任何方法。例如:“未定义方法查找”用于Set:Class“或”undefined methoderrors“用于#”。它似乎试图在Ruby“Set”类而不是我的模型类中找到这些方法。

It might work if I could write the fully-qualified name of my Set model class like Module::Set, but I'm not sure what that would be. (And yes, I really do want my model name Set. Anything else would be awkward in the context of my app).

如果我可以像Module :: Set那样编写我的Set模型类的完全限定名称,它可能会工作,但我不确定那是什么。 (是的,我确实想要我的模型名称Set。在我的应用程序的上下文中,任何其他东西都会很尴尬)。

Any ideas?

1 个解决方案

#1


22  

Don't name it Set. That way lies madness.

不要将它命名为Set。那种方式就是疯狂。

The deal is that defining the class fails because you're trying to redefine 'Set' which is already defined in the global context.

这笔交易是因为你试图重新定义已在全局上下文中定义的“Set”而定义类失败。

class Set < ActiveRecord::Base # You are attempting to define a constant 'Set'
                               # here, but you can't because it already exists

You can put your class in a module, and then you won't get the error because you'll be defining Set within a namespace.

您可以将您的类放在一个模块中,然后您将不会收到错误,因为您将在命名空间中定义Set。

module Custom
  class Set < ActiveRecord::Base
  end
end

However, every single time you want to use your Set class, you'll have to refer to it as Custom::Set. A lot of Rails magic won't work because it's expecting class names to be defined in the global context. You'll be monkeypatching plugins and gems left and right.

但是,每次要使用Set类时,都必须将其称为Custom :: Set。很多Rails魔法都不会起作用,因为它期望在全局上下文中定义类名。你将左右单独插入插件和宝石。

Far easier to just give it a different name.

更容易给它一个不同的名字。

class CustomSet < ActiveRecord::Base

All the magic works, and no monkeypatching required.

所有的魔法都有效,而且不需要monkeypatching。

#1


22  

Don't name it Set. That way lies madness.

不要将它命名为Set。那种方式就是疯狂。

The deal is that defining the class fails because you're trying to redefine 'Set' which is already defined in the global context.

这笔交易是因为你试图重新定义已在全局上下文中定义的“Set”而定义类失败。

class Set < ActiveRecord::Base # You are attempting to define a constant 'Set'
                               # here, but you can't because it already exists

You can put your class in a module, and then you won't get the error because you'll be defining Set within a namespace.

您可以将您的类放在一个模块中,然后您将不会收到错误,因为您将在命名空间中定义Set。

module Custom
  class Set < ActiveRecord::Base
  end
end

However, every single time you want to use your Set class, you'll have to refer to it as Custom::Set. A lot of Rails magic won't work because it's expecting class names to be defined in the global context. You'll be monkeypatching plugins and gems left and right.

但是,每次要使用Set类时,都必须将其称为Custom :: Set。很多Rails魔法都不会起作用,因为它期望在全局上下文中定义类名。你将左右单独插入插件和宝石。

Far easier to just give it a different name.

更容易给它一个不同的名字。

class CustomSet < ActiveRecord::Base

All the magic works, and no monkeypatching required.

所有的魔法都有效,而且不需要monkeypatching。