rudolph9
rudolph9

Reputation: 8129

ActiveRecord: Name already in use within the application, where?

I'm attempting to generate a scaffold but generating it I receive the following error:

rails generate scaffold foo
      invoke  active_record
The name 'Foo' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.

Is there a command to find out exactly where this name is being used within my application?

Upvotes: 1

Views: 2582

Answers (4)

JTE
JTE

Reputation: 1321

Szuper tricky! For me the model name was stuck in memory in the Spring cache system. Had to kill the spring process to free it up.

Look for this when you attempt the command:

Running via Spring preloader in process 57104
Expected string default value for '--serializer'; got true (boolean)
  invoke  active_record
The name 'Admin' is either already used in your application or reserved 
by Ruby on Rails. Please choose an alternative and run this generator 
again.

If you see that Spring comment, try looking for spring in your processes and killing:

ps -ef | grep spring

find the id:

501 54701 30654 0 1:43PM ?? 0:04.83 spring app | server | started 8 mins ago | development mode

501 30654 1 0 Tue03PM ttys000 0:03.82 spring server | server | started 142 hours ago

and kill

kill 30654

Upvotes: 0

Sean
Sean

Reputation: 1118

Using rubymine, there are a few ways you can do this. There is a "find usages" command that will find all location where a method, variable, etc. are used. There is a "jump to declaration" which in your case would be useful. It will jump to the spot where something is defined. (a class, module, variable, method). there is also a powerful search feature. In this case, search in path would allow you to search the entire application (including external gems being used). You can force case sensitivity on your search to only yield class / module names etc.

Upvotes: 0

Naren Sisodiya
Naren Sisodiya

Reputation: 7288

I don't think there is a way to find the file or source of any object/class/module. Also rails has open class concept so the class can be defined or refined in many files so we can not track the same.

but you can check if the name is exist for any object by following

Module.constants.include? "Foo" 

Upvotes: 2

davidb
davidb

Reputation: 8954

Thats just one of the given possibilities! Foo is a reserved Word. Ruby also reserves words that arent used as a Model/Module name already. For example you also cant create a model called Configuration eaven there is no class thats clled Configuration.

Upvotes: -1

Related Questions