Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'rails generate' commands hang when trying to create a model

I'm new to rails and decided this morning to dump my whole database design/model and start over. And being a noob, I'm sure did it incorrectly.

I removed all the files in db/migrate/ and dropped the tables. And when I tried to generate the first new model class, rails just hung. Off in the weeds for 10 minutes before I hit ^C and tried something else.

This time, I again dropped the tables, moved the whole project to project.bad and ran rails new to start over. Again, after generating the new project with the old name, it hung on the rails generate command (I was using the same project name).

In desperation, I tried creating a new project in the same root, but with another name. Eureka! This worked like a champ, creating controllers and model classes, but I'm completely unable to generate anything using the original project name, in the original project or any newly-created one. What am I missing to get this working again? I don't mind a complete loss at this point, but I'd like to be able to use the original project name again!

Here's what log/development.log looks like:

   (255.5ms)  CREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL) ENGINE=InnoDB
   (337.7ms)  CREATE UNIQUE INDEX `unique_schema_migrations`  ON `schema_migrations` (`version`) 
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
   (0.2ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations`

Any idea what's supposed to happen after that last SELECT?

like image 632
Bret Avatar asked Aug 06 '15 13:08

Bret


4 Answers

If your rails generate commands hangs, it is most likely that the generated binstubs of rails are the issue. As you mentioned, you renamed the project.

My educated guess is that some paths in the binstubs were still set to the old project directory but did not exist any longer.

There is a great article on how binstubs work here: https://github.com/sstephenson/rbenv/wiki/Understanding-binstubs

rails 4

To reset the binstubs, just delete your bin/ directory in rails and run:

# generates binstubs for ALL gems in the bundle
bundle install --binstubs

# ...OR, generate binstubs for a SINGLE gem (recommended)
bundle binstubs rake

rails 5/rails 6

To reset the binstubs, just delete your bin/ directory in rails and run:

rake app:update:bin

Why do we need to use the 'rake' command for rails 5 and higher, and not the 'rails' command itself?

Since rails 5 some 'rake' commands are encapsulated within the 'rails' command. However when one deletes 'bin/' directory one is also removeing the 'rails' command itself, thus one needs to go back to 'rake' for the reset since 'rails' is not available any longer but 'rake' still is.

like image 91
mahatmanich Avatar answered Nov 18 '22 04:11

mahatmanich


Found this over at http://www.dixis.com/?p=754

For one of my projects I am using rails 4.1 (bleeding edge! yeah :) ) and suddenly noticed, that after opening my laptop in the morning my normal rails commands, like

$> rails c
$> rails g migration Bla name description some_more_fields

just … were hanging and nothing happened??? Like they were waiting for further input. Upon closer investigation, I assumed that the connection to the spring process was lost/corrupt (I move between networks a lot? maybe that could explain it).

For those unaware, as I was, spring is a Rails application preloader. It speeds up development by keeping your application running in the background so you don’t need to boot it every time you run a test, rake task or migration. Of course when that connection is lost, or corrupt, it hangs.

A simple

$> spring stop

stops the spring server, after which any rails command will restart it automatically. Fixed :)

like image 21
Daniel Avatar answered Nov 18 '22 03:11

Daniel


In Rails 5 the binstups are created using the rails command.

I just deleted the bin folder myself and then ran rails app:update:bin which fixed my problems.

In Rails 5, your app's bin/ directory contains executables that are versioned like any other source code, rather than stubs that are generated on demand.

Here's how to upgrade:

bundle config --delete bin    # Turn off Bundler's stub generator
rails app:update:bin          # Use the new Rails 5 executables
git add bin                   # Add bin/ to source control
like image 12
mtrolle Avatar answered Nov 18 '22 05:11

mtrolle


I just had to kill spring.

Before

$ rails generate yaddi-yaddi-yadda
hang...
hang...
hang..
^C

My fix:

$ ps -u {user} | grep spring
123 123456 spring app ...

Find the pid, then kill spring.

$ rails generate yaddi-yaddi-yadda
# success.
like image 7
Tristan Hall Avatar answered Nov 18 '22 05:11

Tristan Hall