2

I've noticed I've been having to do:

bundle exec script/console
<wait for console to load>
require migration
generate some data

a lot... and I was wondering if there is a way to have this all in a bash script or something. so i could just do ./generatedata and have it run the above commands.

1
  • 1
    Why do you wanna a ruby script? This could be done easily with bash script as a just a sequence of commands. Commented Aug 24, 2012 at 15:23

2 Answers 2

3

I've found that custom rake tasks are an awesome tool for when you have work which requires running code in the rails environment. Check out this railscast http://railscasts.com/episodes/66-custom-rake-tasks

Sign up to request clarification or add additional context in comments.

1 Comment

+1, and quite easy to implement as well--for non-interactive, repeated tasks (hint hint) this is the preferred solution, I'd think. Plus it can be run very quickly via rails-sh.
3

If you want to run a one-off command in the console, you can use the rails runner command. So if you had a ./generatedata.rb script which performs the ruby commands you want to execute in the console, you can just call rails runner ./generatedata.rb and it will run your ruby script in the rails environment against the database. Alternatively, you could add the shebang line to the ./generatedata.rb script: #!/usr/bin/env rails runner. Then you only need to execute the ./generatedata.rb script and it will use rails runner automatically.

2 Comments

Also +1, although I might make it a task. One reason is that as a task it can be run trivially via rake-sh, which means it can be much faster than a command-line rails runner script.
You're right. This is an alternative for when you really do just have a quick task that doesn't really feel like it needs to be a rake task. It doesn't happen very often though, so I agree that usually a rake task would be the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.