'rails generate' commands hang when trying to create a model
Asked Answered
C

7

80

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?

Crud answered 6/8, 2015 at 13:40 Comment(10)
What commands did you issue before rails hung? And what did hang? The webserver? The console?Subtangent
I tried both "rails generate model User" and "rails generate controller welcome index". Both commands just hung. This is so early in my project's development that I could just toss all my current code and start over, but rails doesn't do any better with a freshly-created project if the name is the same as the original (after I've deleted or renamed the original).Crud
did you try to reset the terminal? Close all terminal windows? Might be that the old rails environment is still present. Using rvm or rbenv?Subtangent
Using rbenv. I'll try closing the terminals and getting new ones.Crud
If you renamed it of course you need to also rename the application 'config/application.rb' ... did bundle or bundle install do anything in the old projects. Further did your 'generate' commands end correctly and not send you in some terminal nirvana, e.g. a / at the end of the commandSubtangent
I ran: "[bretw@bw112 aurora]$ rails generate model user" (copied from my fresh terminal), and then nothing. I tried bundle install and rbenv rehash. It hung again. The new log is here: gist.github.com/wortmanb/2d900b819e7a9f15a254Crud
did you rename the app? does 'bundle exec rails generate model user' work?Subtangent
you are in the rails project directory right?Subtangent
In the rails dir, yes. When I renamed to project from "aurora" to "aurora2" and ran "bundle exec rails generate model user", it did create the code as expected. And following up with "rails generate model message" worked as well.Crud
Let us continue this discussion in chat.Crud
S
253

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.

Subtangent answered 6/8, 2015 at 13:50 Comment(12)
I sussed out that I should have gone back to VERSION=0, but what I wanted to do was toss out everything I've done so far, so I deleted everything from db/migrate. I said it was a noob mistake.... I've included the contents of log/development.log above.Crud
Oops, I goofed. The "rails new" worked, but "rails generate model Foo" failed and caused the hang. I've corrected the original question to reflect this.Crud
was db/migrate directory still present on the rails generate?Subtangent
Yes, but empty. Schema.rb exists but is effectively also empty and contains just this: ActiveRecord::Schema.define(version: 0) do endCrud
Binstubs was the answer!Crud
After deleting the bin directory, rails commands no longer worked, so I had to run the command as rake app:update:binFireplace
@Fireplace Yes that is what it says above! Delete bin/ AND run rails app:update:bin to generate a new bin/ and its binaries for Rails 5 that is!Subtangent
@Subtangent What I'm saying is that when I tried to run rails app:update:bin, it did not do anything, other than print many error messages. To get it to work, I had to change rails to rake.Fireplace
@Fireplace what version of rails are you using?Subtangent
@Fireplace Ah makes sense. Since Rails 5 some Rake commands are encapsulated within the rails command. However when you delete bin/ you are also removeing the rails command, thus you gotta go back to rake. I'll make an update. Thanks for the heads up!Subtangent
Thank you! This was my issue. After running rake app:update:bin in Rails 5 the scaffold ran.Dissolve
your solution for rails 5 worked like a charm for me. I was generating a scaffold which was hanging for me; now it works perfectly fine! Thanks a ton!!Ramshackle
L
88

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

Landeros answered 23/6, 2016 at 0:52 Comment(3)
This was exactly my problem. Thanks!Colfin
This fixed my problem. Thanks!!Georgy
same here. running debian using wsl2 on windows 10, in case somebody searches for that.Histology
F
12

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
Federalist answered 29/9, 2016 at 18:23 Comment(0)
D
9

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.
Detrition answered 27/5, 2020 at 14:47 Comment(0)
V
1

TL;DR: Restarting computer worked for me.

I had the same problem, and although the chosen answer worked, I wasn't comfortable deleting a bunch of stuff I admittedly don't fully understand. My git status on the bin dir looked like this, after deleting the bin dir and running rails app:update:bin

deleted:    bin/bundle
modified:   bin/rails
modified:   bin/rake
modified:   bin/setup
deleted:    bin/spring
deleted:    bin/webpack
deleted:    bin/webpack-dev-server
deleted:    bin/yarn

I felt like something might come back to bite me later, so after reading the article referenced in the accepted answer (http://www.dixis.com/?p=754) I decided to just restart my computer, as that would fix any networking issues. It worked like a charm.

Velutinous answered 1/4, 2020 at 23:7 Comment(0)
O
0

closing re-opening the terminal did the trick for me

Orthorhombic answered 28/5, 2021 at 19:10 Comment(0)
H
-1

I had the same problem when trying use rails g controller and it would just hang. I used the same steps @mtrolle suggested:

bundle config --delete bin
rails app:update:bin
git add bin

So when I ran: rails g controller Project index it created the controller, helpers, and index view and GET 'project/index' route as expected.

Hwang answered 11/9, 2017 at 3:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.