Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name is already used or reserved by Ruby on Rails?

I created a rails application with the following command:

rails new Education

Now, I am trying to create a new model in rails with the following command:

rails generate model Education name:string

When running it it returns the following error:

The name 'Education' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.

Since I just created a new app and only have one other model and I have a hard time thinking about any reason why Rails would reserve a name like that?

Any thoughts on where this error comes from and how I can work around it?

(I have tried changing the name to something else and it works as expected. Since the name really fits its purpose I don't want to change its name unless there is no other way!)

I am using Ruby 2.0.0 with Rails 4.0.0 and PostgreSQL

like image 875
Severin Avatar asked Nov 04 '13 21:11

Severin


2 Answers

You may not create a model with the same name as the Application because it will create conflicting names. When you create an application i.e. rails new Education it will create a module named Education as follows

module Education
  class Application < Rails::Application
  #....
  end
end

This named module is then called in files such as config.ru, routes.rb and environment.rb and many more. So if you were able to create a model class by the same name it would create ambiguity as to whether you were calling the Model or the Module.

like image 199
engineersmnky Avatar answered Nov 20 '22 20:11

engineersmnky


Adding two other possible reasons people might get this error.

Firstly you've been changing names and the old name is still around as it's a caching issue.

Secondly, it could be a naming conflict with a gem you have installed. So the gem is already using the name you want to use.

CASE I:: Cache issue

I got below error::

The name 'Activityflow' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.

While running rails g if you see below messages in initial lines. It could be cache issue:

warning: previous definition of CALLBACK_CAMPAIGN_NAME was here
Running via Spring preloader in process 98806
      invoke  active_record

Solution is kill spring and it will freeup cache.

Find process id of spring

$ ps -ef | grep spring
501 82388 82384   0  2:21PM ??        66:34.87 spring app    | insurance | started 16 hours ago | development mode      
501 82384     1   0  2:21PM ttys005    0:00.84 spring server | insurance | started 16 hours ago   

Kill process Eg: 82384 in above case

$ kill -9 82384

Then when you run rails g command again; Spring will run/start with fresh contents and model creation succeed.

Running via Spring preloader in process 99237
  invoke  active_record
  create    db/migrate/20200518021818_create_activityflows.rb
  create    app/models/activityflow.rb
  invoke    rspec
  create      spec/models/activityflow_spec.rb

CASE II:: Module Issue

I got below error::

The name 'Workflow' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.

The issue was I was using gem gem 'workflow', '~> 2.0.2' which creates modules names Workflow and does not allow to generate model named Workflow.

So, be careful when you use any gem and is module name in any gem gets matched with model name.

Simple way to check if module name exist is::
$ bin/rails c
[4] pry(main)> Workflow
=> Workflow
[5] pry(main)> Workflow.class
=> Module
like image 30
silwalprabin Avatar answered Nov 20 '22 22:11

silwalprabin