Rails cannot find a model with the same name as the Ruby class

I am new to Ruby on Rails, and I have a project with the Install model. This is in Rails 2.3.2. Now the problem is that he cannot find any methods in this class of the model at all. For example: "undefined method find' for Set:Class" or "undefined method errors" for # ". It seems to be trying to find these methods in the Ruby class" Set "instead of my model class.

This might work if I can write the full name of my Set model class, such as Module :: Set, but I'm not sure what it will be. (And yes, I really want my Set model name. Everything else would be inconvenient in the context of my application).

Any ideas?

+6
ruby module class ruby-on-rails
source share
1 answer

Do not call it Set. So madness.

The deal is that the class definition is not executed because you are trying to override "Set", which is already defined in the global context.

 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 will not get an error because you will define Set in the namespace.

 module Custom class Set < ActiveRecord::Base end end 

However, every time you want to use your Set class, you will have to refer to it as Custom :: Set. Many Rails magic will not work because they expect class names to be defined in a global context. You will neutralize plugins and gems left and right.

It’s much easier to just give it a different name.

 class CustomSet < ActiveRecord::Base 

All magic works, and no monkeypatching binding is required.

+22
source share

All Articles