10

Rails provides a very useful console ('script/console').

I write a ruby file and run it in the console using require foo.rb.

It works fine the first time, but the second and next require foo.rb does not run my script (require does not reload it).

Do you have any tips/tricks?

2 Answers 2

17

require is used to load extensions - so the code will execute once, to get the extensions to be present in your environment, but subsequent requires won't do anything, because the job has already been done.

load, on the other hand, loads and executes the code every time.

As already mentioned, if you just want to run your script and you need the Rails environment, then consider using script/runner

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

1 Comment

Make sure you use the full file name with load (require doesn't require this ;))
2

You should probably try either loading your rails environment in a script or using rake. Also consider using script/runner.

Here is an old and possibly outdated example of using your rails environment in a script. A more recent and detailed version here.

A stack overflow answer

Comments

Your Answer

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