TomDogg
TomDogg

Reputation: 3937

Rails3: How to execute Rails commands (like "rails generate") with shell script

I need to be able to generate a model (and later a migration) by executing a Linux shell script.

The script is located directly in the app folder and looks like this:

#!/bin/bash

cd /home/<my_user_profile>/Websites/<my_app_name>
rails g model my_model name:string accepted:boolean [etc...]

The problem is: When I execute the script, the model does not get created. Any ideas why?

Upvotes: 1

Views: 482

Answers (2)

Dave Newton
Dave Newton

Reputation: 160170

To make sure it's executing in the same context your shell is in, remove the shebang to avoid starting up another bash which may or may not be the same as your current shell.

If you're using rvm/similar, you'd need to either (a) have a default, (b) specify version/gemset, or (c) rely on rvm-like cd fudgery.

Otherwise, should work just fine--it is for me (w/o the shebang, so it'll use whatever current rvm environment I'm in).

Upvotes: 0

ryudice
ryudice

Reputation: 37366

Try

exec "rails g model my_model name:string accepted:boolean"

Upvotes: 1

Related Questions