views:

75

answers:

2

I have a RoR project on my Windows 7 PC.

I want to create some Ruby code that I can execute from the cmd.exe command line that manipulates the development database (via database.yml) of the project. (I don't want to have to run my utility code via a web page.)

What is the best way to go about pulling this off? (I'm a newbie.)

I can't put the code in the test/ directory because that executes against the test database.

I tried just creating a utility.rb file under app/ but when I run it I get this:

utility.rb:5: uninitialized constant ActiveRecord (NameError)

My standalone file obviously doesn't know about the rest of the rails framework.

Any suggestions?

+1  A: 

Rails comes with a utility to do exactly this. Instead of using ruby filename, use script/runner filename (from within the top-level directory for the Rails project), which will automatically load up your Rails environment before running the script.

However, if what you're trying to do is manipulate the database, the right answer is probably to create a migration. Most people assume that migrations are only for changing the structure of your database (adding or removing columns or tables) but they can also be a great way to add seed data or manipulate all the data in the database.

Emily
That worked great. Thanks!
Pete Alvin
+1  A: 

You can write your own rake task which depends on :environment and pass RAILS_ENV=development when executing it.

Nice screencast about it: screencast

Kylo