views:

482

answers:

1

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 method errors' for #". It seems to be trying to find those methods on the Ruby "Set" class instead my model class.

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).

Any ideas?

+15  A: 

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

The deal is that defining the class fails because you're trying to redefine '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 won't get the error because you'll be defining Set within a namespace.

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.

Far easier to just give it a different name.

class CustomSet < ActiveRecord::Base

All the magic works, and no monkeypatching required.

Sarah Mei
Thanks, I'll have to think of another name.